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