]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/raw_ip.c
MFS r353177:
[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         struct epoch_tracker et;
289         int hash;
290
291         *mp = NULL;
292
293         bzero(&ripsrc, sizeof(ripsrc));
294         ripsrc.sin_len = sizeof(ripsrc);
295         ripsrc.sin_family = AF_INET;
296         ripsrc.sin_addr = ip->ip_src;
297         last = NULL;
298
299         ifp = m->m_pkthdr.rcvif;
300
301         hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
302             ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
303         INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
304         CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
305                 if (inp->inp_ip_p != proto)
306                         continue;
307 #ifdef INET6
308                 /* XXX inp locking */
309                 if ((inp->inp_vflag & INP_IPV4) == 0)
310                         continue;
311 #endif
312                 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
313                         continue;
314                 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
315                         continue;
316                 if (last != NULL) {
317                         struct mbuf *n;
318
319                         n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
320                         if (n != NULL)
321                             (void) rip_append(last, ip, n, &ripsrc);
322                         /* XXX count dropped packet */
323                         INP_RUNLOCK(last);
324                         last = NULL;
325                 }
326                 INP_RLOCK(inp);
327                 if (__predict_false(inp->inp_flags2 & INP_FREED))
328                         goto skip_1;
329                 if (jailed_without_vnet(inp->inp_cred)) {
330                         /*
331                          * XXX: If faddr was bound to multicast group,
332                          * jailed raw socket will drop datagram.
333                          */
334                         if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
335                                 goto skip_1;
336                 }
337                 last = inp;
338                 continue;
339         skip_1:
340                 INP_RUNLOCK(inp);
341         }
342         CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
343                 if (inp->inp_ip_p && inp->inp_ip_p != proto)
344                         continue;
345 #ifdef INET6
346                 /* XXX inp locking */
347                 if ((inp->inp_vflag & INP_IPV4) == 0)
348                         continue;
349 #endif
350                 if (!in_nullhost(inp->inp_laddr) &&
351                     !in_hosteq(inp->inp_laddr, ip->ip_dst))
352                         continue;
353                 if (!in_nullhost(inp->inp_faddr) &&
354                     !in_hosteq(inp->inp_faddr, ip->ip_src))
355                         continue;
356                 if (last != NULL) {
357                         struct mbuf *n;
358
359                         n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
360                         if (n != NULL)
361                                 (void) rip_append(last, ip, n, &ripsrc);
362                         /* XXX count dropped packet */
363                         INP_RUNLOCK(last);
364                         last = NULL;
365                 }
366                 INP_RLOCK(inp);
367                 if (__predict_false(inp->inp_flags2 & INP_FREED))
368                         goto skip_2;
369                 if (jailed_without_vnet(inp->inp_cred)) {
370                         /*
371                          * Allow raw socket in jail to receive multicast;
372                          * assume process had PRIV_NETINET_RAW at attach,
373                          * and fall through into normal filter path if so.
374                          */
375                         if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
376                             prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
377                                 goto skip_2;
378                 }
379                 /*
380                  * If this raw socket has multicast state, and we
381                  * have received a multicast, check if this socket
382                  * should receive it, as multicast filtering is now
383                  * the responsibility of the transport layer.
384                  */
385                 if (inp->inp_moptions != NULL &&
386                     IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
387                         /*
388                          * If the incoming datagram is for IGMP, allow it
389                          * through unconditionally to the raw socket.
390                          *
391                          * In the case of IGMPv2, we may not have explicitly
392                          * joined the group, and may have set IFF_ALLMULTI
393                          * on the interface. imo_multi_filter() may discard
394                          * control traffic we actually need to see.
395                          *
396                          * Userland multicast routing daemons should continue
397                          * filter the control traffic appropriately.
398                          */
399                         int blocked;
400
401                         blocked = MCAST_PASS;
402                         if (proto != IPPROTO_IGMP) {
403                                 struct sockaddr_in group;
404
405                                 bzero(&group, sizeof(struct sockaddr_in));
406                                 group.sin_len = sizeof(struct sockaddr_in);
407                                 group.sin_family = AF_INET;
408                                 group.sin_addr = ip->ip_dst;
409
410                                 blocked = imo_multi_filter(inp->inp_moptions,
411                                     ifp,
412                                     (struct sockaddr *)&group,
413                                     (struct sockaddr *)&ripsrc);
414                         }
415
416                         if (blocked != MCAST_PASS) {
417                                 IPSTAT_INC(ips_notmember);
418                                 goto skip_2;
419                         }
420                 }
421                 last = inp;
422                 continue;
423         skip_2:
424                 INP_RUNLOCK(inp);
425         }
426         INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
427         if (last != NULL) {
428                 if (rip_append(last, ip, m, &ripsrc) != 0)
429                         IPSTAT_INC(ips_delivered);
430                 INP_RUNLOCK(last);
431         } else {
432                 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
433                         IPSTAT_INC(ips_noproto);
434                         IPSTAT_DEC(ips_delivered);
435                         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
436                 } else {
437                         m_freem(m);
438                 }
439         }
440         return (IPPROTO_DONE);
441 }
442
443 /*
444  * Generate IP header and pass packet to ip_output.  Tack on options user may
445  * have setup with control call.
446  */
447 int
448 rip_output(struct mbuf *m, struct socket *so, ...)
449 {
450         struct ip *ip;
451         int error;
452         struct inpcb *inp = sotoinpcb(so);
453         va_list ap;
454         u_long dst;
455         int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
456             IP_ALLOWBROADCAST;
457         int cnt, hlen;
458         u_char opttype, optlen, *cp;
459
460         va_start(ap, so);
461         dst = va_arg(ap, u_long);
462         va_end(ap);
463
464         /*
465          * If the user handed us a complete IP packet, use it.  Otherwise,
466          * allocate an mbuf for a header and fill it in.
467          */
468         if ((inp->inp_flags & INP_HDRINCL) == 0) {
469                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
470                         m_freem(m);
471                         return(EMSGSIZE);
472                 }
473                 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
474                 if (m == NULL)
475                         return(ENOBUFS);
476
477                 INP_RLOCK(inp);
478                 ip = mtod(m, struct ip *);
479                 ip->ip_tos = inp->inp_ip_tos;
480                 if (inp->inp_flags & INP_DONTFRAG)
481                         ip->ip_off = htons(IP_DF);
482                 else
483                         ip->ip_off = htons(0);
484                 ip->ip_p = inp->inp_ip_p;
485                 ip->ip_len = htons(m->m_pkthdr.len);
486                 ip->ip_src = inp->inp_laddr;
487                 ip->ip_dst.s_addr = dst;
488                 if (jailed(inp->inp_cred)) {
489                         /*
490                          * prison_local_ip4() would be good enough but would
491                          * let a source of INADDR_ANY pass, which we do not
492                          * want to see from jails.
493                          */
494                         if (ip->ip_src.s_addr == INADDR_ANY) {
495                                 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
496                                     inp->inp_cred);
497                         } else {
498                                 error = prison_local_ip4(inp->inp_cred,
499                                     &ip->ip_src);
500                         }
501                         if (error != 0) {
502                                 INP_RUNLOCK(inp);
503                                 m_freem(m);
504                                 return (error);
505                         }
506                 }
507                 ip->ip_ttl = inp->inp_ip_ttl;
508         } else {
509                 if (m->m_pkthdr.len > IP_MAXPACKET) {
510                         m_freem(m);
511                         return(EMSGSIZE);
512                 }
513                 ip = mtod(m, struct ip *);
514                 hlen = ip->ip_hl << 2;
515                 if (m->m_len < hlen) {
516                         m = m_pullup(m, hlen);
517                         if (m == NULL)
518                                 return (EINVAL);
519                         ip = mtod(m, struct ip *);
520                 }
521
522                 INP_RLOCK(inp);
523                 /*
524                  * Don't allow both user specified and setsockopt options,
525                  * and don't allow packet length sizes that will crash.
526                  */
527                 if ((hlen < sizeof (*ip))
528                     || ((hlen > sizeof (*ip)) && inp->inp_options)
529                     || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
530                         INP_RUNLOCK(inp);
531                         m_freem(m);
532                         return (EINVAL);
533                 }
534                 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
535                 if (error != 0) {
536                         INP_RUNLOCK(inp);
537                         m_freem(m);
538                         return (error);
539                 }
540                 /*
541                  * Don't allow IP options which do not have the required
542                  * structure as specified in section 3.1 of RFC 791 on
543                  * pages 15-23.
544                  */
545                 cp = (u_char *)(ip + 1);
546                 cnt = hlen - sizeof (struct ip);
547                 for (; cnt > 0; cnt -= optlen, cp += optlen) {
548                         opttype = cp[IPOPT_OPTVAL];
549                         if (opttype == IPOPT_EOL)
550                                 break;
551                         if (opttype == IPOPT_NOP) {
552                                 optlen = 1;
553                                 continue;
554                         }
555                         if (cnt < IPOPT_OLEN + sizeof(u_char)) {
556                                 INP_RUNLOCK(inp);
557                                 m_freem(m);
558                                 return (EINVAL);
559                         }
560                         optlen = cp[IPOPT_OLEN];
561                         if (optlen < IPOPT_OLEN + sizeof(u_char) ||
562                             optlen > cnt) {
563                                 INP_RUNLOCK(inp);
564                                 m_freem(m);
565                                 return (EINVAL);
566                         }
567                 }
568                 /*
569                  * This doesn't allow application to specify ID of zero,
570                  * but we got this limitation from the beginning of history.
571                  */
572                 if (ip->ip_id == 0)
573                         ip_fillid(ip);
574
575                 /*
576                  * XXX prevent ip_output from overwriting header fields.
577                  */
578                 flags |= IP_RAWOUTPUT;
579                 IPSTAT_INC(ips_rawout);
580         }
581
582         if (inp->inp_flags & INP_ONESBCAST)
583                 flags |= IP_SENDONES;
584
585 #ifdef MAC
586         mac_inpcb_create_mbuf(inp, m);
587 #endif
588
589         error = ip_output(m, inp->inp_options, NULL, flags,
590             inp->inp_moptions, inp);
591         INP_RUNLOCK(inp);
592         return (error);
593 }
594
595 /*
596  * Raw IP socket option processing.
597  *
598  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
599  * only be created by a privileged process, and as such, socket option
600  * operations to manage system properties on any raw socket were allowed to
601  * take place without explicit additional access control checks.  However,
602  * raw sockets can now also be created in jail(), and therefore explicit
603  * checks are now required.  Likewise, raw sockets can be used by a process
604  * after it gives up privilege, so some caution is required.  For options
605  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
606  * performed in ip_ctloutput() and therefore no check occurs here.
607  * Unilaterally checking priv_check() here breaks normal IP socket option
608  * operations on raw sockets.
609  *
610  * When adding new socket options here, make sure to add access control
611  * checks here as necessary.
612  *
613  * XXX-BZ inp locking?
614  */
615 int
616 rip_ctloutput(struct socket *so, struct sockopt *sopt)
617 {
618         struct  inpcb *inp = sotoinpcb(so);
619         int     error, optval;
620
621         if (sopt->sopt_level != IPPROTO_IP) {
622                 if ((sopt->sopt_level == SOL_SOCKET) &&
623                     (sopt->sopt_name == SO_SETFIB)) {
624                         inp->inp_inc.inc_fibnum = so->so_fibnum;
625                         return (0);
626                 }
627                 return (EINVAL);
628         }
629
630         error = 0;
631         switch (sopt->sopt_dir) {
632         case SOPT_GET:
633                 switch (sopt->sopt_name) {
634                 case IP_HDRINCL:
635                         optval = inp->inp_flags & INP_HDRINCL;
636                         error = sooptcopyout(sopt, &optval, sizeof optval);
637                         break;
638
639                 case IP_FW3:    /* generic ipfw v.3 functions */
640                 case IP_FW_ADD: /* ADD actually returns the body... */
641                 case IP_FW_GET:
642                 case IP_FW_TABLE_GETSIZE:
643                 case IP_FW_TABLE_LIST:
644                 case IP_FW_NAT_GET_CONFIG:
645                 case IP_FW_NAT_GET_LOG:
646                         if (V_ip_fw_ctl_ptr != NULL)
647                                 error = V_ip_fw_ctl_ptr(sopt);
648                         else
649                                 error = ENOPROTOOPT;
650                         break;
651
652                 case IP_DUMMYNET3:      /* generic dummynet v.3 functions */
653                 case IP_DUMMYNET_GET:
654                         if (ip_dn_ctl_ptr != NULL)
655                                 error = ip_dn_ctl_ptr(sopt);
656                         else
657                                 error = ENOPROTOOPT;
658                         break ;
659
660                 case MRT_INIT:
661                 case MRT_DONE:
662                 case MRT_ADD_VIF:
663                 case MRT_DEL_VIF:
664                 case MRT_ADD_MFC:
665                 case MRT_DEL_MFC:
666                 case MRT_VERSION:
667                 case MRT_ASSERT:
668                 case MRT_API_SUPPORT:
669                 case MRT_API_CONFIG:
670                 case MRT_ADD_BW_UPCALL:
671                 case MRT_DEL_BW_UPCALL:
672                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
673                         if (error != 0)
674                                 return (error);
675                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
676                                 EOPNOTSUPP;
677                         break;
678
679                 default:
680                         error = ip_ctloutput(so, sopt);
681                         break;
682                 }
683                 break;
684
685         case SOPT_SET:
686                 switch (sopt->sopt_name) {
687                 case IP_HDRINCL:
688                         error = sooptcopyin(sopt, &optval, sizeof optval,
689                                             sizeof optval);
690                         if (error)
691                                 break;
692                         if (optval)
693                                 inp->inp_flags |= INP_HDRINCL;
694                         else
695                                 inp->inp_flags &= ~INP_HDRINCL;
696                         break;
697
698                 case IP_FW3:    /* generic ipfw v.3 functions */
699                 case IP_FW_ADD:
700                 case IP_FW_DEL:
701                 case IP_FW_FLUSH:
702                 case IP_FW_ZERO:
703                 case IP_FW_RESETLOG:
704                 case IP_FW_TABLE_ADD:
705                 case IP_FW_TABLE_DEL:
706                 case IP_FW_TABLE_FLUSH:
707                 case IP_FW_NAT_CFG:
708                 case IP_FW_NAT_DEL:
709                         if (V_ip_fw_ctl_ptr != NULL)
710                                 error = V_ip_fw_ctl_ptr(sopt);
711                         else
712                                 error = ENOPROTOOPT;
713                         break;
714
715                 case IP_DUMMYNET3:      /* generic dummynet v.3 functions */
716                 case IP_DUMMYNET_CONFIGURE:
717                 case IP_DUMMYNET_DEL:
718                 case IP_DUMMYNET_FLUSH:
719                         if (ip_dn_ctl_ptr != NULL)
720                                 error = ip_dn_ctl_ptr(sopt);
721                         else
722                                 error = ENOPROTOOPT ;
723                         break ;
724
725                 case IP_RSVP_ON:
726                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
727                         if (error != 0)
728                                 return (error);
729                         error = ip_rsvp_init(so);
730                         break;
731
732                 case IP_RSVP_OFF:
733                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
734                         if (error != 0)
735                                 return (error);
736                         error = ip_rsvp_done();
737                         break;
738
739                 case IP_RSVP_VIF_ON:
740                 case IP_RSVP_VIF_OFF:
741                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
742                         if (error != 0)
743                                 return (error);
744                         error = ip_rsvp_vif ?
745                                 ip_rsvp_vif(so, sopt) : EINVAL;
746                         break;
747
748                 case MRT_INIT:
749                 case MRT_DONE:
750                 case MRT_ADD_VIF:
751                 case MRT_DEL_VIF:
752                 case MRT_ADD_MFC:
753                 case MRT_DEL_MFC:
754                 case MRT_VERSION:
755                 case MRT_ASSERT:
756                 case MRT_API_SUPPORT:
757                 case MRT_API_CONFIG:
758                 case MRT_ADD_BW_UPCALL:
759                 case MRT_DEL_BW_UPCALL:
760                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
761                         if (error != 0)
762                                 return (error);
763                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
764                                         EOPNOTSUPP;
765                         break;
766
767                 default:
768                         error = ip_ctloutput(so, sopt);
769                         break;
770                 }
771                 break;
772         }
773
774         return (error);
775 }
776
777 /*
778  * This function exists solely to receive the PRC_IFDOWN messages which are
779  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
780  * in_ifadown() to remove all routes corresponding to that address.  It also
781  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
782  * routes.
783  */
784 void
785 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
786 {
787         struct rm_priotracker in_ifa_tracker;
788         struct in_ifaddr *ia;
789         struct ifnet *ifp;
790         int err;
791         int flags;
792
793         switch (cmd) {
794         case PRC_IFDOWN:
795                 IN_IFADDR_RLOCK(&in_ifa_tracker);
796                 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
797                         if (ia->ia_ifa.ifa_addr == sa
798                             && (ia->ia_flags & IFA_ROUTE)) {
799                                 ifa_ref(&ia->ia_ifa);
800                                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
801                                 /*
802                                  * in_scrubprefix() kills the interface route.
803                                  */
804                                 in_scrubprefix(ia, 0);
805                                 /*
806                                  * in_ifadown gets rid of all the rest of the
807                                  * routes.  This is not quite the right thing
808                                  * to do, but at least if we are running a
809                                  * routing process they will come back.
810                                  */
811                                 in_ifadown(&ia->ia_ifa, 0);
812                                 ifa_free(&ia->ia_ifa);
813                                 break;
814                         }
815                 }
816                 if (ia == NULL)         /* If ia matched, already unlocked. */
817                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
818                 break;
819
820         case PRC_IFUP:
821                 IN_IFADDR_RLOCK(&in_ifa_tracker);
822                 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
823                         if (ia->ia_ifa.ifa_addr == sa)
824                                 break;
825                 }
826                 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
827                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
828                         return;
829                 }
830                 ifa_ref(&ia->ia_ifa);
831                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
832                 flags = RTF_UP;
833                 ifp = ia->ia_ifa.ifa_ifp;
834
835                 if ((ifp->if_flags & IFF_LOOPBACK)
836                     || (ifp->if_flags & IFF_POINTOPOINT))
837                         flags |= RTF_HOST;
838
839                 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
840
841                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
842                 if (err == 0)
843                         ia->ia_flags |= IFA_ROUTE;
844
845                 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
846
847                 ifa_free(&ia->ia_ifa);
848                 break;
849         }
850 }
851
852 static int
853 rip_attach(struct socket *so, int proto, struct thread *td)
854 {
855         struct inpcb *inp;
856         int error;
857
858         inp = sotoinpcb(so);
859         KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
860
861         error = priv_check(td, PRIV_NETINET_RAW);
862         if (error)
863                 return (error);
864         if (proto >= IPPROTO_MAX || proto < 0)
865                 return EPROTONOSUPPORT;
866         error = soreserve(so, rip_sendspace, rip_recvspace);
867         if (error)
868                 return (error);
869         INP_INFO_WLOCK(&V_ripcbinfo);
870         error = in_pcballoc(so, &V_ripcbinfo);
871         if (error) {
872                 INP_INFO_WUNLOCK(&V_ripcbinfo);
873                 return (error);
874         }
875         inp = (struct inpcb *)so->so_pcb;
876         inp->inp_vflag |= INP_IPV4;
877         inp->inp_ip_p = proto;
878         inp->inp_ip_ttl = V_ip_defttl;
879         rip_inshash(inp);
880         INP_INFO_WUNLOCK(&V_ripcbinfo);
881         INP_WUNLOCK(inp);
882         return (0);
883 }
884
885 static void
886 rip_detach(struct socket *so)
887 {
888         struct inpcb *inp;
889
890         inp = sotoinpcb(so);
891         KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
892         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 
893             ("rip_detach: not closed"));
894
895         INP_INFO_WLOCK(&V_ripcbinfo);
896         INP_WLOCK(inp);
897         rip_delhash(inp);
898         if (so == V_ip_mrouter && ip_mrouter_done)
899                 ip_mrouter_done();
900         if (ip_rsvp_force_done)
901                 ip_rsvp_force_done(so);
902         if (so == V_ip_rsvpd)
903                 ip_rsvp_done();
904         in_pcbdetach(inp);
905         in_pcbfree(inp);
906         INP_INFO_WUNLOCK(&V_ripcbinfo);
907 }
908
909 static void
910 rip_dodisconnect(struct socket *so, struct inpcb *inp)
911 {
912         struct inpcbinfo *pcbinfo;
913
914         pcbinfo = inp->inp_pcbinfo;
915         INP_INFO_WLOCK(pcbinfo);
916         INP_WLOCK(inp);
917         rip_delhash(inp);
918         inp->inp_faddr.s_addr = INADDR_ANY;
919         rip_inshash(inp);
920         SOCK_LOCK(so);
921         so->so_state &= ~SS_ISCONNECTED;
922         SOCK_UNLOCK(so);
923         INP_WUNLOCK(inp);
924         INP_INFO_WUNLOCK(pcbinfo);
925 }
926
927 static void
928 rip_abort(struct socket *so)
929 {
930         struct inpcb *inp;
931
932         inp = sotoinpcb(so);
933         KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
934
935         rip_dodisconnect(so, inp);
936 }
937
938 static void
939 rip_close(struct socket *so)
940 {
941         struct inpcb *inp;
942
943         inp = sotoinpcb(so);
944         KASSERT(inp != NULL, ("rip_close: inp == NULL"));
945
946         rip_dodisconnect(so, inp);
947 }
948
949 static int
950 rip_disconnect(struct socket *so)
951 {
952         struct inpcb *inp;
953
954         if ((so->so_state & SS_ISCONNECTED) == 0)
955                 return (ENOTCONN);
956
957         inp = sotoinpcb(so);
958         KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
959
960         rip_dodisconnect(so, inp);
961         return (0);
962 }
963
964 static int
965 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
966 {
967         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
968         struct inpcb *inp;
969         int error;
970
971         if (nam->sa_len != sizeof(*addr))
972                 return (EINVAL);
973
974         error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
975         if (error != 0)
976                 return (error);
977
978         inp = sotoinpcb(so);
979         KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
980
981         if (CK_STAILQ_EMPTY(&V_ifnet) ||
982             (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
983             (addr->sin_addr.s_addr &&
984              (inp->inp_flags & INP_BINDANY) == 0 &&
985              ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
986                 return (EADDRNOTAVAIL);
987
988         INP_INFO_WLOCK(&V_ripcbinfo);
989         INP_WLOCK(inp);
990         rip_delhash(inp);
991         inp->inp_laddr = addr->sin_addr;
992         rip_inshash(inp);
993         INP_WUNLOCK(inp);
994         INP_INFO_WUNLOCK(&V_ripcbinfo);
995         return (0);
996 }
997
998 static int
999 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1000 {
1001         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1002         struct inpcb *inp;
1003
1004         if (nam->sa_len != sizeof(*addr))
1005                 return (EINVAL);
1006         if (CK_STAILQ_EMPTY(&V_ifnet))
1007                 return (EADDRNOTAVAIL);
1008         if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
1009                 return (EAFNOSUPPORT);
1010
1011         inp = sotoinpcb(so);
1012         KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
1013
1014         INP_INFO_WLOCK(&V_ripcbinfo);
1015         INP_WLOCK(inp);
1016         rip_delhash(inp);
1017         inp->inp_faddr = addr->sin_addr;
1018         rip_inshash(inp);
1019         soisconnected(so);
1020         INP_WUNLOCK(inp);
1021         INP_INFO_WUNLOCK(&V_ripcbinfo);
1022         return (0);
1023 }
1024
1025 static int
1026 rip_shutdown(struct socket *so)
1027 {
1028         struct inpcb *inp;
1029
1030         inp = sotoinpcb(so);
1031         KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1032
1033         INP_WLOCK(inp);
1034         socantsendmore(so);
1035         INP_WUNLOCK(inp);
1036         return (0);
1037 }
1038
1039 static int
1040 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1041     struct mbuf *control, struct thread *td)
1042 {
1043         struct inpcb *inp;
1044         u_long dst;
1045
1046         inp = sotoinpcb(so);
1047         KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1048
1049         /*
1050          * Note: 'dst' reads below are unlocked.
1051          */
1052         if (so->so_state & SS_ISCONNECTED) {
1053                 if (nam) {
1054                         m_freem(m);
1055                         return (EISCONN);
1056                 }
1057                 dst = inp->inp_faddr.s_addr;    /* Unlocked read. */
1058         } else {
1059                 if (nam == NULL) {
1060                         m_freem(m);
1061                         return (ENOTCONN);
1062                 }
1063                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1064         }
1065         return (rip_output(m, so, dst));
1066 }
1067 #endif /* INET */
1068
1069 static int
1070 rip_pcblist(SYSCTL_HANDLER_ARGS)
1071 {
1072         int error, i, n;
1073         struct inpcb *inp, **inp_list;
1074         inp_gen_t gencnt;
1075         struct xinpgen xig;
1076         struct epoch_tracker et;
1077
1078         /*
1079          * The process of preparing the TCB list is too time-consuming and
1080          * resource-intensive to repeat twice on every request.
1081          */
1082         if (req->oldptr == 0) {
1083                 n = V_ripcbinfo.ipi_count;
1084                 n += imax(n / 8, 10);
1085                 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1086                 return (0);
1087         }
1088
1089         if (req->newptr != 0)
1090                 return (EPERM);
1091
1092         /*
1093          * OK, now we're committed to doing something.
1094          */
1095         INP_INFO_WLOCK(&V_ripcbinfo);
1096         gencnt = V_ripcbinfo.ipi_gencnt;
1097         n = V_ripcbinfo.ipi_count;
1098         INP_INFO_WUNLOCK(&V_ripcbinfo);
1099
1100         bzero(&xig, sizeof(xig));
1101         xig.xig_len = sizeof xig;
1102         xig.xig_count = n;
1103         xig.xig_gen = gencnt;
1104         xig.xig_sogen = so_gencnt;
1105         error = SYSCTL_OUT(req, &xig, sizeof xig);
1106         if (error)
1107                 return (error);
1108
1109         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1110
1111         INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
1112         for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1113              inp = CK_LIST_NEXT(inp, inp_list)) {
1114                 INP_WLOCK(inp);
1115                 if (inp->inp_gencnt <= gencnt &&
1116                     cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1117                         in_pcbref(inp);
1118                         inp_list[i++] = inp;
1119                 }
1120                 INP_WUNLOCK(inp);
1121         }
1122         INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
1123         n = i;
1124
1125         error = 0;
1126         for (i = 0; i < n; i++) {
1127                 inp = inp_list[i];
1128                 INP_RLOCK(inp);
1129                 if (inp->inp_gencnt <= gencnt) {
1130                         struct xinpcb xi;
1131
1132                         in_pcbtoxinpcb(inp, &xi);
1133                         INP_RUNLOCK(inp);
1134                         error = SYSCTL_OUT(req, &xi, sizeof xi);
1135                 } else
1136                         INP_RUNLOCK(inp);
1137         }
1138         INP_INFO_WLOCK(&V_ripcbinfo);
1139         for (i = 0; i < n; i++) {
1140                 inp = inp_list[i];
1141                 INP_RLOCK(inp);
1142                 if (!in_pcbrele_rlocked(inp))
1143                         INP_RUNLOCK(inp);
1144         }
1145         INP_INFO_WUNLOCK(&V_ripcbinfo);
1146
1147         if (!error) {
1148                 struct epoch_tracker et;
1149                 /*
1150                  * Give the user an updated idea of our state.  If the
1151                  * generation differs from what we told her before, she knows
1152                  * that something happened while we were processing this
1153                  * request, and it might be necessary to retry.
1154                  */
1155                 INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
1156                 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1157                 xig.xig_sogen = so_gencnt;
1158                 xig.xig_count = V_ripcbinfo.ipi_count;
1159                 INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
1160                 error = SYSCTL_OUT(req, &xig, sizeof xig);
1161         }
1162         free(inp_list, M_TEMP);
1163         return (error);
1164 }
1165
1166 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1167     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1168     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1169
1170 #ifdef INET
1171 struct pr_usrreqs rip_usrreqs = {
1172         .pru_abort =            rip_abort,
1173         .pru_attach =           rip_attach,
1174         .pru_bind =             rip_bind,
1175         .pru_connect =          rip_connect,
1176         .pru_control =          in_control,
1177         .pru_detach =           rip_detach,
1178         .pru_disconnect =       rip_disconnect,
1179         .pru_peeraddr =         in_getpeeraddr,
1180         .pru_send =             rip_send,
1181         .pru_shutdown =         rip_shutdown,
1182         .pru_sockaddr =         in_getsockaddr,
1183         .pru_sosetlabel =       in_pcbsosetlabel,
1184         .pru_close =            rip_close,
1185 };
1186 #endif /* INET */