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