]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/netinet/raw_ip.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / netinet / raw_ip.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
38
39 #include <sys/param.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sx.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55
56 #include <vm/uma.h>
57
58 #include <net/if.h>
59 #include <net/route.h>
60 #include <net/vnet.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_mroute.h>
69
70 #ifdef IPSEC
71 #include <netipsec/ipsec.h>
72 #endif /*IPSEC*/
73
74 #include <security/mac/mac_framework.h>
75
76 VNET_DEFINE(struct inpcbhead, ripcb);
77 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
78
79 #define V_ripcb                 VNET(ripcb)
80 #define V_ripcbinfo             VNET(ripcbinfo)
81
82 /*
83  * Control and data hooks for ipfw and dummynet.
84  * The data hooks are not used here but it is convenient
85  * to keep them all in one place.
86  */
87 int (*ip_fw_ctl_ptr)(struct sockopt *) = NULL;
88 int (*ip_dn_ctl_ptr)(struct sockopt *) = NULL;
89 int (*ip_fw_chk_ptr)(struct ip_fw_args *args) = NULL;
90 int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa) = NULL;
91
92 /*
93  * Hooks for multicast routing. They all default to NULL, so leave them not
94  * initialized and rely on BSS being set to 0.
95  */
96
97 /*
98  * The socket used to communicate with the multicast routing daemon.
99  */
100 VNET_DEFINE(struct socket *, ip_mrouter);
101
102 /*
103  * The various mrouter and rsvp functions.
104  */
105 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
106 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
107 int (*ip_mrouter_done)(void);
108 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
109                    struct ip_moptions *);
110 int (*mrt_ioctl)(u_long, caddr_t, int);
111 int (*legal_vif_num)(int);
112 u_long (*ip_mcast_src)(int);
113
114 void (*rsvp_input_p)(struct mbuf *m, int off);
115 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
116 void (*ip_rsvp_force_done)(struct socket *);
117
118 /*
119  * Hash functions
120  */
121
122 #define INP_PCBHASH_RAW_SIZE    256
123 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
124         (((proto) + (laddr) + (faddr)) % (mask) + 1)
125
126 static void
127 rip_inshash(struct inpcb *inp)
128 {
129         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
130         struct inpcbhead *pcbhash;
131         int hash;
132
133         INP_INFO_WLOCK_ASSERT(pcbinfo);
134         INP_WLOCK_ASSERT(inp);
135         
136         if (inp->inp_ip_p != 0 &&
137             inp->inp_laddr.s_addr != INADDR_ANY &&
138             inp->inp_faddr.s_addr != INADDR_ANY) {
139                 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
140                     inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
141         } else
142                 hash = 0;
143         pcbhash = &pcbinfo->ipi_hashbase[hash];
144         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
145 }
146
147 static void
148 rip_delhash(struct inpcb *inp)
149 {
150
151         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
152         INP_WLOCK_ASSERT(inp);
153
154         LIST_REMOVE(inp, inp_hash);
155 }
156
157 /*
158  * Raw interface to IP protocol.
159  */
160
161 /*
162  * Initialize raw connection block q.
163  */
164 static void
165 rip_zone_change(void *tag)
166 {
167
168         uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
169 }
170
171 static int
172 rip_inpcb_init(void *mem, int size, int flags)
173 {
174         struct inpcb *inp = mem;
175
176         INP_LOCK_INIT(inp, "inp", "rawinp");
177         return (0);
178 }
179
180 void
181 rip_init(void)
182 {
183
184         INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
185         LIST_INIT(&V_ripcb);
186 #ifdef VIMAGE
187         V_ripcbinfo.ipi_vnet = curvnet;
188 #endif
189         V_ripcbinfo.ipi_listhead = &V_ripcb;
190         V_ripcbinfo.ipi_hashbase =
191             hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
192         V_ripcbinfo.ipi_porthashbase =
193             hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
194         V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
195             NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
196         uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
197         EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
198             EVENTHANDLER_PRI_ANY);
199 }
200
201 #ifdef VIMAGE
202 void
203 rip_destroy(void)
204 {
205
206         hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
207             V_ripcbinfo.ipi_hashmask);
208         hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
209             V_ripcbinfo.ipi_porthashmask);
210 }
211 #endif
212
213 static int
214 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
215     struct sockaddr_in *ripsrc)
216 {
217         int policyfail = 0;
218
219         INP_RLOCK_ASSERT(last);
220
221 #ifdef IPSEC
222         /* check AH/ESP integrity. */
223         if (ipsec4_in_reject(n, last)) {
224                 policyfail = 1;
225         }
226 #endif /* IPSEC */
227 #ifdef MAC
228         if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
229                 policyfail = 1;
230 #endif
231         /* Check the minimum TTL for socket. */
232         if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
233                 policyfail = 1;
234         if (!policyfail) {
235                 struct mbuf *opts = NULL;
236                 struct socket *so;
237
238                 so = last->inp_socket;
239                 if ((last->inp_flags & INP_CONTROLOPTS) ||
240                     (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
241                         ip_savecontrol(last, &opts, ip, n);
242                 SOCKBUF_LOCK(&so->so_rcv);
243                 if (sbappendaddr_locked(&so->so_rcv,
244                     (struct sockaddr *)ripsrc, n, opts) == 0) {
245                         /* should notify about lost packet */
246                         m_freem(n);
247                         if (opts)
248                                 m_freem(opts);
249                         SOCKBUF_UNLOCK(&so->so_rcv);
250                 } else
251                         sorwakeup_locked(so);
252         } else
253                 m_freem(n);
254         return (policyfail);
255 }
256
257 /*
258  * Setup generic address and protocol structures for raw_input routine, then
259  * pass them along with mbuf chain.
260  */
261 void
262 rip_input(struct mbuf *m, int off)
263 {
264         struct ifnet *ifp;
265         struct ip *ip = mtod(m, struct ip *);
266         int proto = ip->ip_p;
267         struct inpcb *inp, *last;
268         struct sockaddr_in ripsrc;
269         int hash;
270
271         bzero(&ripsrc, sizeof(ripsrc));
272         ripsrc.sin_len = sizeof(ripsrc);
273         ripsrc.sin_family = AF_INET;
274         ripsrc.sin_addr = ip->ip_src;
275         last = NULL;
276
277         ifp = m->m_pkthdr.rcvif;
278
279         hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
280             ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
281         INP_INFO_RLOCK(&V_ripcbinfo);
282         LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
283                 if (inp->inp_ip_p != proto)
284                         continue;
285 #ifdef INET6
286                 /* XXX inp locking */
287                 if ((inp->inp_vflag & INP_IPV4) == 0)
288                         continue;
289 #endif
290                 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
291                         continue;
292                 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
293                         continue;
294                 if (jailed(inp->inp_cred)) {
295                         /*
296                          * XXX: If faddr was bound to multicast group,
297                          * jailed raw socket will drop datagram.
298                          */
299                         if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
300                                 continue;
301                 }
302                 if (last != NULL) {
303                         struct mbuf *n;
304
305                         n = m_copy(m, 0, (int)M_COPYALL);
306                         if (n != NULL)
307                             (void) rip_append(last, ip, n, &ripsrc);
308                         /* XXX count dropped packet */
309                         INP_RUNLOCK(last);
310                 }
311                 INP_RLOCK(inp);
312                 last = inp;
313         }
314         LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
315                 if (inp->inp_ip_p && inp->inp_ip_p != proto)
316                         continue;
317 #ifdef INET6
318                 /* XXX inp locking */
319                 if ((inp->inp_vflag & INP_IPV4) == 0)
320                         continue;
321 #endif
322                 if (!in_nullhost(inp->inp_laddr) &&
323                     !in_hosteq(inp->inp_laddr, ip->ip_dst))
324                         continue;
325                 if (!in_nullhost(inp->inp_faddr) &&
326                     !in_hosteq(inp->inp_faddr, ip->ip_src))
327                         continue;
328                 if (jailed(inp->inp_cred)) {
329                         /*
330                          * Allow raw socket in jail to receive multicast;
331                          * assume process had PRIV_NETINET_RAW at attach,
332                          * and fall through into normal filter path if so.
333                          */
334                         if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
335                             prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
336                                 continue;
337                 }
338                 /*
339                  * If this raw socket has multicast state, and we
340                  * have received a multicast, check if this socket
341                  * should receive it, as multicast filtering is now
342                  * the responsibility of the transport layer.
343                  */
344                 if (inp->inp_moptions != NULL &&
345                     IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
346                         struct sockaddr_in group;
347                         int blocked;
348
349                         bzero(&group, sizeof(struct sockaddr_in));
350                         group.sin_len = sizeof(struct sockaddr_in);
351                         group.sin_family = AF_INET;
352                         group.sin_addr = ip->ip_dst;
353
354                         blocked = imo_multi_filter(inp->inp_moptions, ifp,
355                             (struct sockaddr *)&group,
356                             (struct sockaddr *)&ripsrc);
357                         if (blocked != MCAST_PASS) {
358                                 IPSTAT_INC(ips_notmember);
359                                 continue;
360                         }
361                 }
362                 if (last != NULL) {
363                         struct mbuf *n;
364
365                         n = m_copy(m, 0, (int)M_COPYALL);
366                         if (n != NULL)
367                                 (void) rip_append(last, ip, n, &ripsrc);
368                         /* XXX count dropped packet */
369                         INP_RUNLOCK(last);
370                 }
371                 INP_RLOCK(inp);
372                 last = inp;
373         }
374         INP_INFO_RUNLOCK(&V_ripcbinfo);
375         if (last != NULL) {
376                 if (rip_append(last, ip, m, &ripsrc) != 0)
377                         IPSTAT_INC(ips_delivered);
378                 INP_RUNLOCK(last);
379         } else {
380                 m_freem(m);
381                 IPSTAT_INC(ips_noproto);
382                 IPSTAT_DEC(ips_delivered);
383         }
384 }
385
386 /*
387  * Generate IP header and pass packet to ip_output.  Tack on options user may
388  * have setup with control call.
389  */
390 int
391 rip_output(struct mbuf *m, struct socket *so, u_long dst)
392 {
393         struct ip *ip;
394         int error;
395         struct inpcb *inp = sotoinpcb(so);
396         int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
397             IP_ALLOWBROADCAST;
398
399         /*
400          * If the user handed us a complete IP packet, use it.  Otherwise,
401          * allocate an mbuf for a header and fill it in.
402          */
403         if ((inp->inp_flags & INP_HDRINCL) == 0) {
404                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
405                         m_freem(m);
406                         return(EMSGSIZE);
407                 }
408                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
409                 if (m == NULL)
410                         return(ENOBUFS);
411
412                 INP_RLOCK(inp);
413                 ip = mtod(m, struct ip *);
414                 ip->ip_tos = inp->inp_ip_tos;
415                 if (inp->inp_flags & INP_DONTFRAG)
416                         ip->ip_off = IP_DF;
417                 else
418                         ip->ip_off = 0;
419                 ip->ip_p = inp->inp_ip_p;
420                 ip->ip_len = m->m_pkthdr.len;
421                 ip->ip_src = inp->inp_laddr;
422                 error = prison_get_ip4(inp->inp_cred, &ip->ip_src);
423                 if (error != 0) {
424                         INP_RUNLOCK(inp);
425                         m_freem(m);
426                         return (error);
427                 }
428                 ip->ip_dst.s_addr = dst;
429                 ip->ip_ttl = inp->inp_ip_ttl;
430         } else {
431                 if (m->m_pkthdr.len > IP_MAXPACKET) {
432                         m_freem(m);
433                         return(EMSGSIZE);
434                 }
435                 INP_RLOCK(inp);
436                 ip = mtod(m, struct ip *);
437                 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
438                 if (error != 0) {
439                         INP_RUNLOCK(inp);
440                         m_freem(m);
441                         return (error);
442                 }
443
444                 /*
445                  * Don't allow both user specified and setsockopt options,
446                  * and don't allow packet length sizes that will crash.
447                  */
448                 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
449                     || (ip->ip_len > m->m_pkthdr.len)
450                     || (ip->ip_len < (ip->ip_hl << 2))) {
451                         INP_RUNLOCK(inp);
452                         m_freem(m);
453                         return (EINVAL);
454                 }
455                 if (ip->ip_id == 0)
456                         ip->ip_id = ip_newid();
457
458                 /*
459                  * XXX prevent ip_output from overwriting header fields.
460                  */
461                 flags |= IP_RAWOUTPUT;
462                 IPSTAT_INC(ips_rawout);
463         }
464
465         if (inp->inp_flags & INP_ONESBCAST)
466                 flags |= IP_SENDONES;
467
468 #ifdef MAC
469         mac_inpcb_create_mbuf(inp, m);
470 #endif
471
472         error = ip_output(m, inp->inp_options, NULL, flags,
473             inp->inp_moptions, inp);
474         INP_RUNLOCK(inp);
475         return (error);
476 }
477
478 /*
479  * Raw IP socket option processing.
480  *
481  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
482  * only be created by a privileged process, and as such, socket option
483  * operations to manage system properties on any raw socket were allowed to
484  * take place without explicit additional access control checks.  However,
485  * raw sockets can now also be created in jail(), and therefore explicit
486  * checks are now required.  Likewise, raw sockets can be used by a process
487  * after it gives up privilege, so some caution is required.  For options
488  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
489  * performed in ip_ctloutput() and therefore no check occurs here.
490  * Unilaterally checking priv_check() here breaks normal IP socket option
491  * operations on raw sockets.
492  *
493  * When adding new socket options here, make sure to add access control
494  * checks here as necessary.
495  */
496 int
497 rip_ctloutput(struct socket *so, struct sockopt *sopt)
498 {
499         struct  inpcb *inp = sotoinpcb(so);
500         int     error, optval;
501
502         if (sopt->sopt_level != IPPROTO_IP) {
503                 if ((sopt->sopt_level == SOL_SOCKET) &&
504                     (sopt->sopt_name == SO_SETFIB)) {
505                         inp->inp_inc.inc_fibnum = so->so_fibnum;
506                         return (0);
507                 }
508                 return (EINVAL);
509         }
510
511         error = 0;
512         switch (sopt->sopt_dir) {
513         case SOPT_GET:
514                 switch (sopt->sopt_name) {
515                 case IP_HDRINCL:
516                         optval = inp->inp_flags & INP_HDRINCL;
517                         error = sooptcopyout(sopt, &optval, sizeof optval);
518                         break;
519
520                 case IP_FW_ADD: /* ADD actually returns the body... */
521                 case IP_FW_GET:
522                 case IP_FW_TABLE_GETSIZE:
523                 case IP_FW_TABLE_LIST:
524                 case IP_FW_NAT_GET_CONFIG:
525                 case IP_FW_NAT_GET_LOG:
526                         if (ip_fw_ctl_ptr != NULL)
527                                 error = ip_fw_ctl_ptr(sopt);
528                         else
529                                 error = ENOPROTOOPT;
530                         break;
531
532                 case IP_DUMMYNET_GET:
533                         if (ip_dn_ctl_ptr != NULL)
534                                 error = ip_dn_ctl_ptr(sopt);
535                         else
536                                 error = ENOPROTOOPT;
537                         break ;
538
539                 case MRT_INIT:
540                 case MRT_DONE:
541                 case MRT_ADD_VIF:
542                 case MRT_DEL_VIF:
543                 case MRT_ADD_MFC:
544                 case MRT_DEL_MFC:
545                 case MRT_VERSION:
546                 case MRT_ASSERT:
547                 case MRT_API_SUPPORT:
548                 case MRT_API_CONFIG:
549                 case MRT_ADD_BW_UPCALL:
550                 case MRT_DEL_BW_UPCALL:
551                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
552                         if (error != 0)
553                                 return (error);
554                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
555                                 EOPNOTSUPP;
556                         break;
557
558                 default:
559                         error = ip_ctloutput(so, sopt);
560                         break;
561                 }
562                 break;
563
564         case SOPT_SET:
565                 switch (sopt->sopt_name) {
566                 case IP_HDRINCL:
567                         error = sooptcopyin(sopt, &optval, sizeof optval,
568                                             sizeof optval);
569                         if (error)
570                                 break;
571                         if (optval)
572                                 inp->inp_flags |= INP_HDRINCL;
573                         else
574                                 inp->inp_flags &= ~INP_HDRINCL;
575                         break;
576
577                 case IP_FW_ADD:
578                 case IP_FW_DEL:
579                 case IP_FW_FLUSH:
580                 case IP_FW_ZERO:
581                 case IP_FW_RESETLOG:
582                 case IP_FW_TABLE_ADD:
583                 case IP_FW_TABLE_DEL:
584                 case IP_FW_TABLE_FLUSH:
585                 case IP_FW_NAT_CFG:
586                 case IP_FW_NAT_DEL:
587                         if (ip_fw_ctl_ptr != NULL)
588                                 error = ip_fw_ctl_ptr(sopt);
589                         else
590                                 error = ENOPROTOOPT;
591                         break;
592
593                 case IP_DUMMYNET_CONFIGURE:
594                 case IP_DUMMYNET_DEL:
595                 case IP_DUMMYNET_FLUSH:
596                         if (ip_dn_ctl_ptr != NULL)
597                                 error = ip_dn_ctl_ptr(sopt);
598                         else
599                                 error = ENOPROTOOPT ;
600                         break ;
601
602                 case IP_RSVP_ON:
603                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
604                         if (error != 0)
605                                 return (error);
606                         error = ip_rsvp_init(so);
607                         break;
608
609                 case IP_RSVP_OFF:
610                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
611                         if (error != 0)
612                                 return (error);
613                         error = ip_rsvp_done();
614                         break;
615
616                 case IP_RSVP_VIF_ON:
617                 case IP_RSVP_VIF_OFF:
618                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
619                         if (error != 0)
620                                 return (error);
621                         error = ip_rsvp_vif ?
622                                 ip_rsvp_vif(so, sopt) : EINVAL;
623                         break;
624
625                 case MRT_INIT:
626                 case MRT_DONE:
627                 case MRT_ADD_VIF:
628                 case MRT_DEL_VIF:
629                 case MRT_ADD_MFC:
630                 case MRT_DEL_MFC:
631                 case MRT_VERSION:
632                 case MRT_ASSERT:
633                 case MRT_API_SUPPORT:
634                 case MRT_API_CONFIG:
635                 case MRT_ADD_BW_UPCALL:
636                 case MRT_DEL_BW_UPCALL:
637                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
638                         if (error != 0)
639                                 return (error);
640                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
641                                         EOPNOTSUPP;
642                         break;
643
644                 default:
645                         error = ip_ctloutput(so, sopt);
646                         break;
647                 }
648                 break;
649         }
650
651         return (error);
652 }
653
654 /*
655  * This function exists solely to receive the PRC_IFDOWN messages which are
656  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
657  * in_ifadown() to remove all routes corresponding to that address.  It also
658  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
659  * routes.
660  */
661 void
662 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
663 {
664         struct in_ifaddr *ia;
665         struct ifnet *ifp;
666         int err;
667         int flags;
668
669         switch (cmd) {
670         case PRC_IFDOWN:
671                 IN_IFADDR_RLOCK();
672                 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
673                         if (ia->ia_ifa.ifa_addr == sa
674                             && (ia->ia_flags & IFA_ROUTE)) {
675                                 ifa_ref(&ia->ia_ifa);
676                                 IN_IFADDR_RUNLOCK();
677                                 /*
678                                  * in_ifscrub kills the interface route.
679                                  */
680                                 in_ifscrub(ia->ia_ifp, ia);
681                                 /*
682                                  * in_ifadown gets rid of all the rest of the
683                                  * routes.  This is not quite the right thing
684                                  * to do, but at least if we are running a
685                                  * routing process they will come back.
686                                  */
687                                 in_ifadown(&ia->ia_ifa, 0);
688                                 ifa_free(&ia->ia_ifa);
689                                 break;
690                         }
691                 }
692                 if (ia == NULL)         /* If ia matched, already unlocked. */
693                         IN_IFADDR_RUNLOCK();
694                 break;
695
696         case PRC_IFUP:
697                 IN_IFADDR_RLOCK();
698                 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
699                         if (ia->ia_ifa.ifa_addr == sa)
700                                 break;
701                 }
702                 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
703                         IN_IFADDR_RUNLOCK();
704                         return;
705                 }
706                 ifa_ref(&ia->ia_ifa);
707                 IN_IFADDR_RUNLOCK();
708                 flags = RTF_UP;
709                 ifp = ia->ia_ifa.ifa_ifp;
710
711                 if ((ifp->if_flags & IFF_LOOPBACK)
712                     || (ifp->if_flags & IFF_POINTOPOINT))
713                         flags |= RTF_HOST;
714
715                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
716                 if (err == 0)
717                         ia->ia_flags |= IFA_ROUTE;
718                 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
719                 ifa_free(&ia->ia_ifa);
720                 break;
721         }
722 }
723
724 u_long  rip_sendspace = 9216;
725 u_long  rip_recvspace = 9216;
726
727 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
728     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
729 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
730     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
731
732 static int
733 rip_attach(struct socket *so, int proto, struct thread *td)
734 {
735         struct inpcb *inp;
736         int error;
737
738         inp = sotoinpcb(so);
739         KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
740
741         error = priv_check(td, PRIV_NETINET_RAW);
742         if (error)
743                 return (error);
744         if (proto >= IPPROTO_MAX || proto < 0)
745                 return EPROTONOSUPPORT;
746         error = soreserve(so, rip_sendspace, rip_recvspace);
747         if (error)
748                 return (error);
749         INP_INFO_WLOCK(&V_ripcbinfo);
750         error = in_pcballoc(so, &V_ripcbinfo);
751         if (error) {
752                 INP_INFO_WUNLOCK(&V_ripcbinfo);
753                 return (error);
754         }
755         inp = (struct inpcb *)so->so_pcb;
756         inp->inp_vflag |= INP_IPV4;
757         inp->inp_ip_p = proto;
758         inp->inp_ip_ttl = V_ip_defttl;
759         rip_inshash(inp);
760         INP_INFO_WUNLOCK(&V_ripcbinfo);
761         INP_WUNLOCK(inp);
762         return (0);
763 }
764
765 static void
766 rip_detach(struct socket *so)
767 {
768         struct inpcb *inp;
769
770         inp = sotoinpcb(so);
771         KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
772         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 
773             ("rip_detach: not closed"));
774
775         INP_INFO_WLOCK(&V_ripcbinfo);
776         INP_WLOCK(inp);
777         rip_delhash(inp);
778         if (so == V_ip_mrouter && ip_mrouter_done)
779                 ip_mrouter_done();
780         if (ip_rsvp_force_done)
781                 ip_rsvp_force_done(so);
782         if (so == V_ip_rsvpd)
783                 ip_rsvp_done();
784         in_pcbdetach(inp);
785         in_pcbfree(inp);
786         INP_INFO_WUNLOCK(&V_ripcbinfo);
787 }
788
789 static void
790 rip_dodisconnect(struct socket *so, struct inpcb *inp)
791 {
792
793         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
794         INP_WLOCK_ASSERT(inp);
795
796         rip_delhash(inp);
797         inp->inp_faddr.s_addr = INADDR_ANY;
798         rip_inshash(inp);
799         SOCK_LOCK(so);
800         so->so_state &= ~SS_ISCONNECTED;
801         SOCK_UNLOCK(so);
802 }
803
804 static void
805 rip_abort(struct socket *so)
806 {
807         struct inpcb *inp;
808
809         inp = sotoinpcb(so);
810         KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
811
812         INP_INFO_WLOCK(&V_ripcbinfo);
813         INP_WLOCK(inp);
814         rip_dodisconnect(so, inp);
815         INP_WUNLOCK(inp);
816         INP_INFO_WUNLOCK(&V_ripcbinfo);
817 }
818
819 static void
820 rip_close(struct socket *so)
821 {
822         struct inpcb *inp;
823
824         inp = sotoinpcb(so);
825         KASSERT(inp != NULL, ("rip_close: inp == NULL"));
826
827         INP_INFO_WLOCK(&V_ripcbinfo);
828         INP_WLOCK(inp);
829         rip_dodisconnect(so, inp);
830         INP_WUNLOCK(inp);
831         INP_INFO_WUNLOCK(&V_ripcbinfo);
832 }
833
834 static int
835 rip_disconnect(struct socket *so)
836 {
837         struct inpcb *inp;
838
839         if ((so->so_state & SS_ISCONNECTED) == 0)
840                 return (ENOTCONN);
841
842         inp = sotoinpcb(so);
843         KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
844
845         INP_INFO_WLOCK(&V_ripcbinfo);
846         INP_WLOCK(inp);
847         rip_dodisconnect(so, inp);
848         INP_WUNLOCK(inp);
849         INP_INFO_WUNLOCK(&V_ripcbinfo);
850         return (0);
851 }
852
853 static int
854 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
855 {
856         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
857         struct inpcb *inp;
858         int error;
859
860         if (nam->sa_len != sizeof(*addr))
861                 return (EINVAL);
862
863         error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
864         if (error != 0)
865                 return (error);
866
867         inp = sotoinpcb(so);
868         KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
869
870         if (TAILQ_EMPTY(&V_ifnet) ||
871             (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
872             (addr->sin_addr.s_addr &&
873              (inp->inp_flags & INP_BINDANY) == 0 &&
874              ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
875                 return (EADDRNOTAVAIL);
876
877         INP_INFO_WLOCK(&V_ripcbinfo);
878         INP_WLOCK(inp);
879         rip_delhash(inp);
880         inp->inp_laddr = addr->sin_addr;
881         rip_inshash(inp);
882         INP_WUNLOCK(inp);
883         INP_INFO_WUNLOCK(&V_ripcbinfo);
884         return (0);
885 }
886
887 static int
888 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
889 {
890         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
891         struct inpcb *inp;
892
893         if (nam->sa_len != sizeof(*addr))
894                 return (EINVAL);
895         if (TAILQ_EMPTY(&V_ifnet))
896                 return (EADDRNOTAVAIL);
897         if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
898                 return (EAFNOSUPPORT);
899
900         inp = sotoinpcb(so);
901         KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
902
903         INP_INFO_WLOCK(&V_ripcbinfo);
904         INP_WLOCK(inp);
905         rip_delhash(inp);
906         inp->inp_faddr = addr->sin_addr;
907         rip_inshash(inp);
908         soisconnected(so);
909         INP_WUNLOCK(inp);
910         INP_INFO_WUNLOCK(&V_ripcbinfo);
911         return (0);
912 }
913
914 static int
915 rip_shutdown(struct socket *so)
916 {
917         struct inpcb *inp;
918
919         inp = sotoinpcb(so);
920         KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
921
922         INP_WLOCK(inp);
923         socantsendmore(so);
924         INP_WUNLOCK(inp);
925         return (0);
926 }
927
928 static int
929 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
930     struct mbuf *control, struct thread *td)
931 {
932         struct inpcb *inp;
933         u_long dst;
934
935         inp = sotoinpcb(so);
936         KASSERT(inp != NULL, ("rip_send: inp == NULL"));
937
938         /*
939          * Note: 'dst' reads below are unlocked.
940          */
941         if (so->so_state & SS_ISCONNECTED) {
942                 if (nam) {
943                         m_freem(m);
944                         return (EISCONN);
945                 }
946                 dst = inp->inp_faddr.s_addr;    /* Unlocked read. */
947         } else {
948                 if (nam == NULL) {
949                         m_freem(m);
950                         return (ENOTCONN);
951                 }
952                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
953         }
954         return (rip_output(m, so, dst));
955 }
956
957 static int
958 rip_pcblist(SYSCTL_HANDLER_ARGS)
959 {
960         int error, i, n;
961         struct inpcb *inp, **inp_list;
962         inp_gen_t gencnt;
963         struct xinpgen xig;
964
965         /*
966          * The process of preparing the TCB list is too time-consuming and
967          * resource-intensive to repeat twice on every request.
968          */
969         if (req->oldptr == 0) {
970                 n = V_ripcbinfo.ipi_count;
971                 req->oldidx = 2 * (sizeof xig)
972                     + (n + n/8) * sizeof(struct xinpcb);
973                 return (0);
974         }
975
976         if (req->newptr != 0)
977                 return (EPERM);
978
979         /*
980          * OK, now we're committed to doing something.
981          */
982         INP_INFO_RLOCK(&V_ripcbinfo);
983         gencnt = V_ripcbinfo.ipi_gencnt;
984         n = V_ripcbinfo.ipi_count;
985         INP_INFO_RUNLOCK(&V_ripcbinfo);
986
987         xig.xig_len = sizeof xig;
988         xig.xig_count = n;
989         xig.xig_gen = gencnt;
990         xig.xig_sogen = so_gencnt;
991         error = SYSCTL_OUT(req, &xig, sizeof xig);
992         if (error)
993                 return (error);
994
995         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
996         if (inp_list == 0)
997                 return (ENOMEM);
998
999         INP_INFO_RLOCK(&V_ripcbinfo);
1000         for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1001              inp = LIST_NEXT(inp, inp_list)) {
1002                 INP_RLOCK(inp);
1003                 if (inp->inp_gencnt <= gencnt &&
1004                     cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1005                         /* XXX held references? */
1006                         inp_list[i++] = inp;
1007                 }
1008                 INP_RUNLOCK(inp);
1009         }
1010         INP_INFO_RUNLOCK(&V_ripcbinfo);
1011         n = i;
1012
1013         error = 0;
1014         for (i = 0; i < n; i++) {
1015                 inp = inp_list[i];
1016                 INP_RLOCK(inp);
1017                 if (inp->inp_gencnt <= gencnt) {
1018                         struct xinpcb xi;
1019
1020                         bzero(&xi, sizeof(xi));
1021                         xi.xi_len = sizeof xi;
1022                         /* XXX should avoid extra copy */
1023                         bcopy(inp, &xi.xi_inp, sizeof *inp);
1024                         if (inp->inp_socket)
1025                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1026                         INP_RUNLOCK(inp);
1027                         error = SYSCTL_OUT(req, &xi, sizeof xi);
1028                 } else
1029                         INP_RUNLOCK(inp);
1030         }
1031         if (!error) {
1032                 /*
1033                  * Give the user an updated idea of our state.  If the
1034                  * generation differs from what we told her before, she knows
1035                  * that something happened while we were processing this
1036                  * request, and it might be necessary to retry.
1037                  */
1038                 INP_INFO_RLOCK(&V_ripcbinfo);
1039                 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1040                 xig.xig_sogen = so_gencnt;
1041                 xig.xig_count = V_ripcbinfo.ipi_count;
1042                 INP_INFO_RUNLOCK(&V_ripcbinfo);
1043                 error = SYSCTL_OUT(req, &xig, sizeof xig);
1044         }
1045         free(inp_list, M_TEMP);
1046         return (error);
1047 }
1048
1049 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
1050     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1051
1052 struct pr_usrreqs rip_usrreqs = {
1053         .pru_abort =            rip_abort,
1054         .pru_attach =           rip_attach,
1055         .pru_bind =             rip_bind,
1056         .pru_connect =          rip_connect,
1057         .pru_control =          in_control,
1058         .pru_detach =           rip_detach,
1059         .pru_disconnect =       rip_disconnect,
1060         .pru_peeraddr =         in_getpeeraddr,
1061         .pru_send =             rip_send,
1062         .pru_shutdown =         rip_shutdown,
1063         .pru_sockaddr =         in_getsockaddr,
1064         .pru_sosetlabel =       in_pcbsosetlabel,
1065         .pru_close =            rip_close,
1066 };