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