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