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