raw_ip.c revision 1.170 1 /* $NetBSD: raw_ip.c,v 1.170 2018/02/28 11:10:22 maxv Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
61 */
62
63 /*
64 * Raw interface to IP protocol.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.170 2018/02/28 11:10:22 maxv Exp $");
69
70 #ifdef _KERNEL_OPT
71 #include "opt_inet.h"
72 #include "opt_ipsec.h"
73 #include "opt_mrouting.h"
74 #include "opt_net_mpsafe.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/sysctl.h>
79 #include <sys/mbuf.h>
80 #include <sys/socket.h>
81 #include <sys/protosw.h>
82 #include <sys/socketvar.h>
83 #include <sys/errno.h>
84 #include <sys/systm.h>
85 #include <sys/proc.h>
86 #include <sys/kauth.h>
87
88 #include <net/if.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_var.h>
94 #include <netinet/ip_private.h>
95 #include <netinet/ip_mroute.h>
96 #include <netinet/ip_icmp.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/in_proto.h>
99 #include <netinet/in_var.h>
100
101 #ifdef IPSEC
102 #include <netipsec/ipsec.h>
103 #include <netipsec/ipsec_var.h>
104 #include <netipsec/ipsec_private.h>
105 #endif /* IPSEC */
106
107 struct inpcbtable rawcbtable;
108
109 int rip_pcbnotify(struct inpcbtable *, struct in_addr,
110 struct in_addr, int, int, void (*)(struct inpcb *, int));
111 static int rip_connect_pcb(struct inpcb *, struct sockaddr_in *);
112 static void rip_disconnect1(struct inpcb *);
113
114 static void sysctl_net_inet_raw_setup(struct sysctllog **);
115
116 /*
117 * Nominal space allocated to a raw ip socket.
118 */
119 #define RIPSNDQ 8192
120 #define RIPRCVQ 8192
121
122 static u_long rip_sendspace = RIPSNDQ;
123 static u_long rip_recvspace = RIPRCVQ;
124
125 /*
126 * Raw interface to IP protocol.
127 */
128
129 /*
130 * Initialize raw connection block q.
131 */
132 void
133 rip_init(void)
134 {
135
136 sysctl_net_inet_raw_setup(NULL);
137 in_pcbinit(&rawcbtable, 1, 1);
138 }
139
140 static void
141 rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
142 int hlen, struct mbuf *opts, struct mbuf *n)
143 {
144 if (last->inp_flags & INP_NOHEADER)
145 m_adj(n, hlen);
146 if (last->inp_flags & INP_CONTROLOPTS
147 || SOOPT_TIMESTAMP(last->inp_socket->so_options))
148 ip_savecontrol(last, &opts, ip, n);
149 if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
150 /* should notify about lost packet */
151 m_freem(n);
152 if (opts)
153 m_freem(opts);
154 } else
155 sorwakeup(last->inp_socket);
156 }
157
158 /*
159 * Setup generic address and protocol structures
160 * for raw_input routine, then pass them along with
161 * mbuf chain.
162 */
163 void
164 rip_input(struct mbuf *m, ...)
165 {
166 int hlen, proto;
167 struct ip *ip = mtod(m, struct ip *);
168 struct inpcb_hdr *inph;
169 struct inpcb *inp;
170 struct inpcb *last = NULL;
171 struct mbuf *n, *opts = NULL;
172 struct sockaddr_in ripsrc;
173 va_list ap;
174
175 va_start(ap, m);
176 (void)va_arg(ap, int); /* ignore value, advance ap */
177 proto = va_arg(ap, int);
178 va_end(ap);
179
180 sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
181
182 /*
183 * XXX Compatibility: programs using raw IP expect ip_len
184 * XXX to have the header length subtracted, and in host order.
185 * XXX ip_off is also expected to be host order.
186 */
187 hlen = ip->ip_hl << 2;
188 ip->ip_len = ntohs(ip->ip_len) - hlen;
189 NTOHS(ip->ip_off);
190
191 TAILQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
192 inp = (struct inpcb *)inph;
193 if (inp->inp_af != AF_INET)
194 continue;
195 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
196 continue;
197 if (!in_nullhost(inp->inp_laddr) &&
198 !in_hosteq(inp->inp_laddr, ip->ip_dst))
199 continue;
200 if (!in_nullhost(inp->inp_faddr) &&
201 !in_hosteq(inp->inp_faddr, ip->ip_src))
202 continue;
203 if (last == NULL)
204 ;
205 #if defined(IPSEC)
206 /* check AH/ESP integrity. */
207 else if (ipsec_used && ipsec_in_reject(m, last)) {
208 /* do not inject data to pcb */
209 }
210 #endif /*IPSEC*/
211 else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
212 rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
213 n);
214 opts = NULL;
215 }
216 last = inp;
217 }
218 #if defined(IPSEC)
219 /* check AH/ESP integrity. */
220 if (ipsec_used && last != NULL && ipsec_in_reject(m, last)) {
221 m_freem(m);
222 IP_STATDEC(IP_STAT_DELIVERED);
223 /* do not inject data to pcb */
224 } else
225 #endif /*IPSEC*/
226 if (last != NULL)
227 rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
228 else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
229 uint64_t *ips;
230
231 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
232 0, 0);
233 ips = IP_STAT_GETREF();
234 ips[IP_STAT_NOPROTO]++;
235 ips[IP_STAT_DELIVERED]--;
236 IP_STAT_PUTREF();
237 } else
238 m_freem(m);
239 return;
240 }
241
242 int
243 rip_pcbnotify(struct inpcbtable *table,
244 struct in_addr faddr, struct in_addr laddr, int proto, int errno,
245 void (*notify)(struct inpcb *, int))
246 {
247 struct inpcb_hdr *inph, *ninph;
248 int nmatch;
249
250 nmatch = 0;
251 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
252 struct inpcb *inp = (struct inpcb *)inph;
253 if (inp->inp_af != AF_INET)
254 continue;
255 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
256 continue;
257 if (in_hosteq(inp->inp_faddr, faddr) &&
258 in_hosteq(inp->inp_laddr, laddr)) {
259 (*notify)(inp, errno);
260 nmatch++;
261 }
262 }
263
264 return nmatch;
265 }
266
267 void *
268 rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
269 {
270 struct ip *ip = v;
271 void (*notify)(struct inpcb *, int) = in_rtchange;
272 int errno;
273
274 if (sa->sa_family != AF_INET ||
275 sa->sa_len != sizeof(struct sockaddr_in))
276 return NULL;
277 if ((unsigned)cmd >= PRC_NCMDS)
278 return NULL;
279 errno = inetctlerrmap[cmd];
280 if (PRC_IS_REDIRECT(cmd))
281 notify = in_rtchange, ip = 0;
282 else if (cmd == PRC_HOSTDEAD)
283 ip = 0;
284 else if (errno == 0)
285 return NULL;
286 if (ip) {
287 rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
288 ip->ip_src, ip->ip_p, errno, notify);
289
290 /* XXX mapped address case */
291 } else
292 in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
293 notify);
294 return NULL;
295 }
296
297 /*
298 * Generate IP header and pass packet to ip_output.
299 * Tack on options user may have setup with control call.
300 */
301 int
302 rip_output(struct mbuf *m, struct inpcb *inp, struct mbuf *control,
303 struct lwp *l)
304 {
305 struct ip *ip;
306 struct mbuf *opts;
307 struct ip_pktopts pktopts;
308 kauth_cred_t cred;
309 int error, flags;
310
311 flags = (inp->inp_socket->so_options & SO_DONTROUTE) |
312 IP_ALLOWBROADCAST | IP_RETURNMTU;
313
314 if (l == NULL)
315 cred = NULL;
316 else
317 cred = l->l_cred;
318
319 /* Setup IP outgoing packet options */
320 memset(&pktopts, 0, sizeof(pktopts));
321 error = ip_setpktopts(control, &pktopts, &flags, inp, cred);
322 if (control != NULL)
323 m_freem(control);
324 if (error != 0)
325 goto release;
326
327 /*
328 * If the user handed us a complete IP packet, use it.
329 * Otherwise, allocate an mbuf for a header and fill it in.
330 */
331 if ((inp->inp_flags & INP_HDRINCL) == 0) {
332 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
333 error = EMSGSIZE;
334 goto release;
335 }
336 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
337 if (!m) {
338 error = ENOBUFS;
339 goto release;
340 }
341 ip = mtod(m, struct ip *);
342 ip->ip_tos = 0;
343 ip->ip_off = htons(0);
344 ip->ip_p = inp->inp_ip.ip_p;
345 ip->ip_len = htons(m->m_pkthdr.len);
346 ip->ip_src = pktopts.ippo_laddr.sin_addr;
347 ip->ip_dst = inp->inp_faddr;
348 ip->ip_ttl = MAXTTL;
349 opts = inp->inp_options;
350 } else {
351 if (m->m_pkthdr.len > IP_MAXPACKET) {
352 error = EMSGSIZE;
353 goto release;
354 }
355 ip = mtod(m, struct ip *);
356
357 /*
358 * If the mbuf is read-only, we need to allocate
359 * a new mbuf for the header, since we need to
360 * modify the header.
361 */
362 if (M_READONLY(m)) {
363 int hlen = ip->ip_hl << 2;
364
365 m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
366 if (m == NULL) {
367 error = ENOMEM; /* XXX */
368 goto release;
369 }
370 ip = mtod(m, struct ip *);
371 }
372
373 /* XXX userland passes ip_len and ip_off in host order */
374 if (m->m_pkthdr.len != ip->ip_len) {
375 error = EINVAL;
376 goto release;
377 }
378 HTONS(ip->ip_len);
379 HTONS(ip->ip_off);
380 if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
381 flags |= IP_NOIPNEWID;
382 opts = NULL;
383 /* XXX prevent ip_output from overwriting header fields */
384 flags |= IP_RAWOUTPUT;
385 IP_STATINC(IP_STAT_RAWOUT);
386 }
387
388 /*
389 * IP output. Note: if IP_RETURNMTU flag is set, the MTU size
390 * will be stored in inp_errormtu.
391 */
392 return ip_output(m, opts, &inp->inp_route, flags, pktopts.ippo_imo,
393 inp);
394
395 release:
396 if (m != NULL)
397 m_freem(m);
398 return error;
399 }
400
401 /*
402 * Raw IP socket option processing.
403 */
404 int
405 rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
406 {
407 struct inpcb *inp = sotoinpcb(so);
408 int error = 0;
409 int optval;
410
411 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
412 if (op == PRCO_GETOPT) {
413 optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
414 error = sockopt_set(sopt, &optval, sizeof(optval));
415 } else if (op == PRCO_SETOPT) {
416 error = sockopt_getint(sopt, &optval);
417 if (error)
418 goto out;
419 if (optval) {
420 inp->inp_flags &= ~INP_HDRINCL;
421 inp->inp_flags |= INP_NOHEADER;
422 } else
423 inp->inp_flags &= ~INP_NOHEADER;
424 }
425 goto out;
426 } else if (sopt->sopt_level != IPPROTO_IP)
427 return ip_ctloutput(op, so, sopt);
428
429 switch (op) {
430
431 case PRCO_SETOPT:
432 switch (sopt->sopt_name) {
433 case IP_HDRINCL:
434 error = sockopt_getint(sopt, &optval);
435 if (error)
436 break;
437 if (optval)
438 inp->inp_flags |= INP_HDRINCL;
439 else
440 inp->inp_flags &= ~INP_HDRINCL;
441 break;
442
443 #ifdef MROUTING
444 case MRT_INIT:
445 case MRT_DONE:
446 case MRT_ADD_VIF:
447 case MRT_DEL_VIF:
448 case MRT_ADD_MFC:
449 case MRT_DEL_MFC:
450 case MRT_ASSERT:
451 case MRT_API_CONFIG:
452 case MRT_ADD_BW_UPCALL:
453 case MRT_DEL_BW_UPCALL:
454 error = ip_mrouter_set(so, sopt);
455 break;
456 #endif
457
458 default:
459 error = ip_ctloutput(op, so, sopt);
460 break;
461 }
462 break;
463
464 case PRCO_GETOPT:
465 switch (sopt->sopt_name) {
466 case IP_HDRINCL:
467 optval = inp->inp_flags & INP_HDRINCL;
468 error = sockopt_set(sopt, &optval, sizeof(optval));
469 break;
470
471 #ifdef MROUTING
472 case MRT_VERSION:
473 case MRT_ASSERT:
474 case MRT_API_SUPPORT:
475 case MRT_API_CONFIG:
476 error = ip_mrouter_get(so, sopt);
477 break;
478 #endif
479
480 default:
481 error = ip_ctloutput(op, so, sopt);
482 break;
483 }
484 break;
485 }
486 out:
487 return error;
488 }
489
490 int
491 rip_connect_pcb(struct inpcb *inp, struct sockaddr_in *addr)
492 {
493
494 if (IFNET_READER_EMPTY())
495 return (EADDRNOTAVAIL);
496 if (addr->sin_family != AF_INET)
497 return (EAFNOSUPPORT);
498 inp->inp_faddr = addr->sin_addr;
499 return (0);
500 }
501
502 static void
503 rip_disconnect1(struct inpcb *inp)
504 {
505
506 inp->inp_faddr = zeroin_addr;
507 }
508
509 static int
510 rip_attach(struct socket *so, int proto)
511 {
512 struct inpcb *inp;
513 int error;
514
515 KASSERT(sotoinpcb(so) == NULL);
516 sosetlock(so);
517
518 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
519 error = soreserve(so, rip_sendspace, rip_recvspace);
520 if (error) {
521 return error;
522 }
523 }
524
525 error = in_pcballoc(so, &rawcbtable);
526 if (error) {
527 return error;
528 }
529 inp = sotoinpcb(so);
530 inp->inp_ip.ip_p = proto;
531 KASSERT(solocked(so));
532
533 return 0;
534 }
535
536 static void
537 rip_detach(struct socket *so)
538 {
539 struct inpcb *inp;
540
541 KASSERT(solocked(so));
542 inp = sotoinpcb(so);
543 KASSERT(inp != NULL);
544
545 #ifdef MROUTING
546 extern struct socket *ip_mrouter;
547 if (so == ip_mrouter) {
548 ip_mrouter_done();
549 }
550 #endif
551 in_pcbdetach(inp);
552 }
553
554 static int
555 rip_accept(struct socket *so, struct sockaddr *nam)
556 {
557 KASSERT(solocked(so));
558
559 panic("rip_accept");
560
561 return EOPNOTSUPP;
562 }
563
564 static int
565 rip_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
566 {
567 struct inpcb *inp = sotoinpcb(so);
568 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
569 int error = 0;
570 int s, ss;
571 struct ifaddr *ifa;
572
573 KASSERT(solocked(so));
574 KASSERT(inp != NULL);
575 KASSERT(nam != NULL);
576
577 if (addr->sin_len != sizeof(*addr))
578 return EINVAL;
579
580 s = splsoftnet();
581 if (IFNET_READER_EMPTY()) {
582 error = EADDRNOTAVAIL;
583 goto release;
584 }
585 if (addr->sin_family != AF_INET) {
586 error = EAFNOSUPPORT;
587 goto release;
588 }
589 ss = pserialize_read_enter();
590 if ((ifa = ifa_ifwithaddr(sintosa(addr))) == NULL &&
591 !in_nullhost(addr->sin_addr))
592 {
593 pserialize_read_exit(ss);
594 error = EADDRNOTAVAIL;
595 goto release;
596 }
597 if (ifa && (ifatoia(ifa))->ia4_flags & IN6_IFF_DUPLICATED) {
598 pserialize_read_exit(ss);
599 error = EADDRNOTAVAIL;
600 goto release;
601 }
602 pserialize_read_exit(ss);
603
604 inp->inp_laddr = addr->sin_addr;
605
606 release:
607 splx(s);
608 return error;
609 }
610
611 static int
612 rip_listen(struct socket *so, struct lwp *l)
613 {
614 KASSERT(solocked(so));
615
616 return EOPNOTSUPP;
617 }
618
619 static int
620 rip_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
621 {
622 struct inpcb *inp = sotoinpcb(so);
623 int error = 0;
624 int s;
625
626 KASSERT(solocked(so));
627 KASSERT(inp != NULL);
628 KASSERT(nam != NULL);
629
630 s = splsoftnet();
631 error = rip_connect_pcb(inp, (struct sockaddr_in *)nam);
632 if (! error)
633 soisconnected(so);
634 splx(s);
635
636 return error;
637 }
638
639 static int
640 rip_connect2(struct socket *so, struct socket *so2)
641 {
642 KASSERT(solocked(so));
643
644 return EOPNOTSUPP;
645 }
646
647 static int
648 rip_disconnect(struct socket *so)
649 {
650 struct inpcb *inp = sotoinpcb(so);
651 int s;
652
653 KASSERT(solocked(so));
654 KASSERT(inp != NULL);
655
656 s = splsoftnet();
657 soisdisconnected(so);
658 rip_disconnect1(inp);
659 splx(s);
660
661 return 0;
662 }
663
664 static int
665 rip_shutdown(struct socket *so)
666 {
667 int s;
668
669 KASSERT(solocked(so));
670
671 /*
672 * Mark the connection as being incapable of further input.
673 */
674 s = splsoftnet();
675 socantsendmore(so);
676 splx(s);
677
678 return 0;
679 }
680
681 static int
682 rip_abort(struct socket *so)
683 {
684 KASSERT(solocked(so));
685
686 panic("rip_abort");
687
688 return EOPNOTSUPP;
689 }
690
691 static int
692 rip_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
693 {
694 return in_control(so, cmd, nam, ifp);
695 }
696
697 static int
698 rip_stat(struct socket *so, struct stat *ub)
699 {
700 KASSERT(solocked(so));
701
702 /* stat: don't bother with a blocksize. */
703 return 0;
704 }
705
706 static int
707 rip_peeraddr(struct socket *so, struct sockaddr *nam)
708 {
709 int s;
710
711 KASSERT(solocked(so));
712 KASSERT(sotoinpcb(so) != NULL);
713 KASSERT(nam != NULL);
714
715 s = splsoftnet();
716 in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
717 splx(s);
718
719 return 0;
720 }
721
722 static int
723 rip_sockaddr(struct socket *so, struct sockaddr *nam)
724 {
725 int s;
726
727 KASSERT(solocked(so));
728 KASSERT(sotoinpcb(so) != NULL);
729 KASSERT(nam != NULL);
730
731 s = splsoftnet();
732 in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
733 splx(s);
734
735 return 0;
736 }
737
738 static int
739 rip_rcvd(struct socket *so, int flags, struct lwp *l)
740 {
741 KASSERT(solocked(so));
742
743 return EOPNOTSUPP;
744 }
745
746 static int
747 rip_recvoob(struct socket *so, struct mbuf *m, int flags)
748 {
749 KASSERT(solocked(so));
750
751 return EOPNOTSUPP;
752 }
753
754 static int
755 rip_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
756 struct mbuf *control, struct lwp *l)
757 {
758 struct inpcb *inp = sotoinpcb(so);
759 int error = 0;
760 int s;
761
762 KASSERT(solocked(so));
763 KASSERT(inp != NULL);
764 KASSERT(m != NULL);
765
766 /*
767 * Ship a packet out. The appropriate raw output
768 * routine handles any massaging necessary.
769 */
770 s = splsoftnet();
771 if (nam) {
772 if ((so->so_state & SS_ISCONNECTED) != 0) {
773 error = EISCONN;
774 goto die;
775 }
776 error = rip_connect_pcb(inp, (struct sockaddr_in *)nam);
777 if (error)
778 goto die;
779 } else {
780 if ((so->so_state & SS_ISCONNECTED) == 0) {
781 error = ENOTCONN;
782 goto die;
783 }
784 }
785 error = rip_output(m, inp, control, l);
786 m = NULL;
787 control = NULL;
788 if (nam)
789 rip_disconnect1(inp);
790 die:
791 if (m != NULL)
792 m_freem(m);
793 if (control != NULL)
794 m_freem(control);
795
796 splx(s);
797 return error;
798 }
799
800 static int
801 rip_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
802 {
803 KASSERT(solocked(so));
804
805 m_freem(m);
806 m_freem(control);
807
808 return EOPNOTSUPP;
809 }
810
811 static int
812 rip_purgeif(struct socket *so, struct ifnet *ifp)
813 {
814 int s;
815
816 s = splsoftnet();
817 mutex_enter(softnet_lock);
818 in_pcbpurgeif0(&rawcbtable, ifp);
819 #ifdef NET_MPSAFE
820 mutex_exit(softnet_lock);
821 #endif
822 in_purgeif(ifp);
823 #ifdef NET_MPSAFE
824 mutex_enter(softnet_lock);
825 #endif
826 in_pcbpurgeif(&rawcbtable, ifp);
827 mutex_exit(softnet_lock);
828 splx(s);
829
830 return 0;
831 }
832
833 PR_WRAP_USRREQS(rip)
834 #define rip_attach rip_attach_wrapper
835 #define rip_detach rip_detach_wrapper
836 #define rip_accept rip_accept_wrapper
837 #define rip_bind rip_bind_wrapper
838 #define rip_listen rip_listen_wrapper
839 #define rip_connect rip_connect_wrapper
840 #define rip_connect2 rip_connect2_wrapper
841 #define rip_disconnect rip_disconnect_wrapper
842 #define rip_shutdown rip_shutdown_wrapper
843 #define rip_abort rip_abort_wrapper
844 #define rip_ioctl rip_ioctl_wrapper
845 #define rip_stat rip_stat_wrapper
846 #define rip_peeraddr rip_peeraddr_wrapper
847 #define rip_sockaddr rip_sockaddr_wrapper
848 #define rip_rcvd rip_rcvd_wrapper
849 #define rip_recvoob rip_recvoob_wrapper
850 #define rip_send rip_send_wrapper
851 #define rip_sendoob rip_sendoob_wrapper
852 #define rip_purgeif rip_purgeif_wrapper
853
854 const struct pr_usrreqs rip_usrreqs = {
855 .pr_attach = rip_attach,
856 .pr_detach = rip_detach,
857 .pr_accept = rip_accept,
858 .pr_bind = rip_bind,
859 .pr_listen = rip_listen,
860 .pr_connect = rip_connect,
861 .pr_connect2 = rip_connect2,
862 .pr_disconnect = rip_disconnect,
863 .pr_shutdown = rip_shutdown,
864 .pr_abort = rip_abort,
865 .pr_ioctl = rip_ioctl,
866 .pr_stat = rip_stat,
867 .pr_peeraddr = rip_peeraddr,
868 .pr_sockaddr = rip_sockaddr,
869 .pr_rcvd = rip_rcvd,
870 .pr_recvoob = rip_recvoob,
871 .pr_send = rip_send,
872 .pr_sendoob = rip_sendoob,
873 .pr_purgeif = rip_purgeif,
874 };
875
876 static void
877 sysctl_net_inet_raw_setup(struct sysctllog **clog)
878 {
879
880 sysctl_createv(clog, 0, NULL, NULL,
881 CTLFLAG_PERMANENT,
882 CTLTYPE_NODE, "inet", NULL,
883 NULL, 0, NULL, 0,
884 CTL_NET, PF_INET, CTL_EOL);
885 sysctl_createv(clog, 0, NULL, NULL,
886 CTLFLAG_PERMANENT,
887 CTLTYPE_NODE, "raw",
888 SYSCTL_DESCR("Raw IPv4 settings"),
889 NULL, 0, NULL, 0,
890 CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
891
892 sysctl_createv(clog, 0, NULL, NULL,
893 CTLFLAG_PERMANENT,
894 CTLTYPE_STRUCT, "pcblist",
895 SYSCTL_DESCR("Raw IPv4 control block list"),
896 sysctl_inpcblist, 0, &rawcbtable, 0,
897 CTL_NET, PF_INET, IPPROTO_RAW,
898 CTL_CREATE, CTL_EOL);
899 }
900