raw_ip.c revision 1.169 1 /* $NetBSD: raw_ip.c,v 1.169 2018/02/26 09:04:29 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.169 2018/02/26 09:04:29 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 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
209 /* do not inject data to pcb */
210 }
211 #endif /*IPSEC*/
212 else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
213 rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
214 n);
215 opts = NULL;
216 }
217 last = inp;
218 }
219 #if defined(IPSEC)
220 /* check AH/ESP integrity. */
221 if (ipsec_used && last != NULL && ipsec_in_reject(m, last)) {
222 m_freem(m);
223 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
224 IP_STATDEC(IP_STAT_DELIVERED);
225 /* do not inject data to pcb */
226 } else
227 #endif /*IPSEC*/
228 if (last != NULL)
229 rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
230 else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
231 uint64_t *ips;
232
233 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
234 0, 0);
235 ips = IP_STAT_GETREF();
236 ips[IP_STAT_NOPROTO]++;
237 ips[IP_STAT_DELIVERED]--;
238 IP_STAT_PUTREF();
239 } else
240 m_freem(m);
241 return;
242 }
243
244 int
245 rip_pcbnotify(struct inpcbtable *table,
246 struct in_addr faddr, struct in_addr laddr, int proto, int errno,
247 void (*notify)(struct inpcb *, int))
248 {
249 struct inpcb_hdr *inph, *ninph;
250 int nmatch;
251
252 nmatch = 0;
253 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
254 struct inpcb *inp = (struct inpcb *)inph;
255 if (inp->inp_af != AF_INET)
256 continue;
257 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
258 continue;
259 if (in_hosteq(inp->inp_faddr, faddr) &&
260 in_hosteq(inp->inp_laddr, laddr)) {
261 (*notify)(inp, errno);
262 nmatch++;
263 }
264 }
265
266 return nmatch;
267 }
268
269 void *
270 rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
271 {
272 struct ip *ip = v;
273 void (*notify)(struct inpcb *, int) = in_rtchange;
274 int errno;
275
276 if (sa->sa_family != AF_INET ||
277 sa->sa_len != sizeof(struct sockaddr_in))
278 return NULL;
279 if ((unsigned)cmd >= PRC_NCMDS)
280 return NULL;
281 errno = inetctlerrmap[cmd];
282 if (PRC_IS_REDIRECT(cmd))
283 notify = in_rtchange, ip = 0;
284 else if (cmd == PRC_HOSTDEAD)
285 ip = 0;
286 else if (errno == 0)
287 return NULL;
288 if (ip) {
289 rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
290 ip->ip_src, ip->ip_p, errno, notify);
291
292 /* XXX mapped address case */
293 } else
294 in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
295 notify);
296 return NULL;
297 }
298
299 /*
300 * Generate IP header and pass packet to ip_output.
301 * Tack on options user may have setup with control call.
302 */
303 int
304 rip_output(struct mbuf *m, struct inpcb *inp, struct mbuf *control,
305 struct lwp *l)
306 {
307 struct ip *ip;
308 struct mbuf *opts;
309 struct ip_pktopts pktopts;
310 kauth_cred_t cred;
311 int error, flags;
312
313 flags = (inp->inp_socket->so_options & SO_DONTROUTE) |
314 IP_ALLOWBROADCAST | IP_RETURNMTU;
315
316 if (l == NULL)
317 cred = NULL;
318 else
319 cred = l->l_cred;
320
321 /* Setup IP outgoing packet options */
322 memset(&pktopts, 0, sizeof(pktopts));
323 error = ip_setpktopts(control, &pktopts, &flags, inp, cred);
324 if (control != NULL)
325 m_freem(control);
326 if (error != 0)
327 goto release;
328
329 /*
330 * If the user handed us a complete IP packet, use it.
331 * Otherwise, allocate an mbuf for a header and fill it in.
332 */
333 if ((inp->inp_flags & INP_HDRINCL) == 0) {
334 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
335 error = EMSGSIZE;
336 goto release;
337 }
338 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
339 if (!m) {
340 error = ENOBUFS;
341 goto release;
342 }
343 ip = mtod(m, struct ip *);
344 ip->ip_tos = 0;
345 ip->ip_off = htons(0);
346 ip->ip_p = inp->inp_ip.ip_p;
347 ip->ip_len = htons(m->m_pkthdr.len);
348 ip->ip_src = pktopts.ippo_laddr.sin_addr;
349 ip->ip_dst = inp->inp_faddr;
350 ip->ip_ttl = MAXTTL;
351 opts = inp->inp_options;
352 } else {
353 if (m->m_pkthdr.len > IP_MAXPACKET) {
354 error = EMSGSIZE;
355 goto release;
356 }
357 ip = mtod(m, struct ip *);
358
359 /*
360 * If the mbuf is read-only, we need to allocate
361 * a new mbuf for the header, since we need to
362 * modify the header.
363 */
364 if (M_READONLY(m)) {
365 int hlen = ip->ip_hl << 2;
366
367 m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
368 if (m == NULL) {
369 error = ENOMEM; /* XXX */
370 goto release;
371 }
372 ip = mtod(m, struct ip *);
373 }
374
375 /* XXX userland passes ip_len and ip_off in host order */
376 if (m->m_pkthdr.len != ip->ip_len) {
377 error = EINVAL;
378 goto release;
379 }
380 HTONS(ip->ip_len);
381 HTONS(ip->ip_off);
382 if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
383 flags |= IP_NOIPNEWID;
384 opts = NULL;
385 /* XXX prevent ip_output from overwriting header fields */
386 flags |= IP_RAWOUTPUT;
387 IP_STATINC(IP_STAT_RAWOUT);
388 }
389
390 /*
391 * IP output. Note: if IP_RETURNMTU flag is set, the MTU size
392 * will be stored in inp_errormtu.
393 */
394 return ip_output(m, opts, &inp->inp_route, flags, pktopts.ippo_imo,
395 inp);
396
397 release:
398 if (m != NULL)
399 m_freem(m);
400 return error;
401 }
402
403 /*
404 * Raw IP socket option processing.
405 */
406 int
407 rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
408 {
409 struct inpcb *inp = sotoinpcb(so);
410 int error = 0;
411 int optval;
412
413 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
414 if (op == PRCO_GETOPT) {
415 optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
416 error = sockopt_set(sopt, &optval, sizeof(optval));
417 } else if (op == PRCO_SETOPT) {
418 error = sockopt_getint(sopt, &optval);
419 if (error)
420 goto out;
421 if (optval) {
422 inp->inp_flags &= ~INP_HDRINCL;
423 inp->inp_flags |= INP_NOHEADER;
424 } else
425 inp->inp_flags &= ~INP_NOHEADER;
426 }
427 goto out;
428 } else if (sopt->sopt_level != IPPROTO_IP)
429 return ip_ctloutput(op, so, sopt);
430
431 switch (op) {
432
433 case PRCO_SETOPT:
434 switch (sopt->sopt_name) {
435 case IP_HDRINCL:
436 error = sockopt_getint(sopt, &optval);
437 if (error)
438 break;
439 if (optval)
440 inp->inp_flags |= INP_HDRINCL;
441 else
442 inp->inp_flags &= ~INP_HDRINCL;
443 break;
444
445 #ifdef MROUTING
446 case MRT_INIT:
447 case MRT_DONE:
448 case MRT_ADD_VIF:
449 case MRT_DEL_VIF:
450 case MRT_ADD_MFC:
451 case MRT_DEL_MFC:
452 case MRT_ASSERT:
453 case MRT_API_CONFIG:
454 case MRT_ADD_BW_UPCALL:
455 case MRT_DEL_BW_UPCALL:
456 error = ip_mrouter_set(so, sopt);
457 break;
458 #endif
459
460 default:
461 error = ip_ctloutput(op, so, sopt);
462 break;
463 }
464 break;
465
466 case PRCO_GETOPT:
467 switch (sopt->sopt_name) {
468 case IP_HDRINCL:
469 optval = inp->inp_flags & INP_HDRINCL;
470 error = sockopt_set(sopt, &optval, sizeof(optval));
471 break;
472
473 #ifdef MROUTING
474 case MRT_VERSION:
475 case MRT_ASSERT:
476 case MRT_API_SUPPORT:
477 case MRT_API_CONFIG:
478 error = ip_mrouter_get(so, sopt);
479 break;
480 #endif
481
482 default:
483 error = ip_ctloutput(op, so, sopt);
484 break;
485 }
486 break;
487 }
488 out:
489 return error;
490 }
491
492 int
493 rip_connect_pcb(struct inpcb *inp, struct sockaddr_in *addr)
494 {
495
496 if (IFNET_READER_EMPTY())
497 return (EADDRNOTAVAIL);
498 if (addr->sin_family != AF_INET)
499 return (EAFNOSUPPORT);
500 inp->inp_faddr = addr->sin_addr;
501 return (0);
502 }
503
504 static void
505 rip_disconnect1(struct inpcb *inp)
506 {
507
508 inp->inp_faddr = zeroin_addr;
509 }
510
511 static int
512 rip_attach(struct socket *so, int proto)
513 {
514 struct inpcb *inp;
515 int error;
516
517 KASSERT(sotoinpcb(so) == NULL);
518 sosetlock(so);
519
520 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
521 error = soreserve(so, rip_sendspace, rip_recvspace);
522 if (error) {
523 return error;
524 }
525 }
526
527 error = in_pcballoc(so, &rawcbtable);
528 if (error) {
529 return error;
530 }
531 inp = sotoinpcb(so);
532 inp->inp_ip.ip_p = proto;
533 KASSERT(solocked(so));
534
535 return 0;
536 }
537
538 static void
539 rip_detach(struct socket *so)
540 {
541 struct inpcb *inp;
542
543 KASSERT(solocked(so));
544 inp = sotoinpcb(so);
545 KASSERT(inp != NULL);
546
547 #ifdef MROUTING
548 extern struct socket *ip_mrouter;
549 if (so == ip_mrouter) {
550 ip_mrouter_done();
551 }
552 #endif
553 in_pcbdetach(inp);
554 }
555
556 static int
557 rip_accept(struct socket *so, struct sockaddr *nam)
558 {
559 KASSERT(solocked(so));
560
561 panic("rip_accept");
562
563 return EOPNOTSUPP;
564 }
565
566 static int
567 rip_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
568 {
569 struct inpcb *inp = sotoinpcb(so);
570 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
571 int error = 0;
572 int s, ss;
573 struct ifaddr *ifa;
574
575 KASSERT(solocked(so));
576 KASSERT(inp != NULL);
577 KASSERT(nam != NULL);
578
579 if (addr->sin_len != sizeof(*addr))
580 return EINVAL;
581
582 s = splsoftnet();
583 if (IFNET_READER_EMPTY()) {
584 error = EADDRNOTAVAIL;
585 goto release;
586 }
587 if (addr->sin_family != AF_INET) {
588 error = EAFNOSUPPORT;
589 goto release;
590 }
591 ss = pserialize_read_enter();
592 if ((ifa = ifa_ifwithaddr(sintosa(addr))) == NULL &&
593 !in_nullhost(addr->sin_addr))
594 {
595 pserialize_read_exit(ss);
596 error = EADDRNOTAVAIL;
597 goto release;
598 }
599 if (ifa && (ifatoia(ifa))->ia4_flags & IN6_IFF_DUPLICATED) {
600 pserialize_read_exit(ss);
601 error = EADDRNOTAVAIL;
602 goto release;
603 }
604 pserialize_read_exit(ss);
605
606 inp->inp_laddr = addr->sin_addr;
607
608 release:
609 splx(s);
610 return error;
611 }
612
613 static int
614 rip_listen(struct socket *so, struct lwp *l)
615 {
616 KASSERT(solocked(so));
617
618 return EOPNOTSUPP;
619 }
620
621 static int
622 rip_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
623 {
624 struct inpcb *inp = sotoinpcb(so);
625 int error = 0;
626 int s;
627
628 KASSERT(solocked(so));
629 KASSERT(inp != NULL);
630 KASSERT(nam != NULL);
631
632 s = splsoftnet();
633 error = rip_connect_pcb(inp, (struct sockaddr_in *)nam);
634 if (! error)
635 soisconnected(so);
636 splx(s);
637
638 return error;
639 }
640
641 static int
642 rip_connect2(struct socket *so, struct socket *so2)
643 {
644 KASSERT(solocked(so));
645
646 return EOPNOTSUPP;
647 }
648
649 static int
650 rip_disconnect(struct socket *so)
651 {
652 struct inpcb *inp = sotoinpcb(so);
653 int s;
654
655 KASSERT(solocked(so));
656 KASSERT(inp != NULL);
657
658 s = splsoftnet();
659 soisdisconnected(so);
660 rip_disconnect1(inp);
661 splx(s);
662
663 return 0;
664 }
665
666 static int
667 rip_shutdown(struct socket *so)
668 {
669 int s;
670
671 KASSERT(solocked(so));
672
673 /*
674 * Mark the connection as being incapable of further input.
675 */
676 s = splsoftnet();
677 socantsendmore(so);
678 splx(s);
679
680 return 0;
681 }
682
683 static int
684 rip_abort(struct socket *so)
685 {
686 KASSERT(solocked(so));
687
688 panic("rip_abort");
689
690 return EOPNOTSUPP;
691 }
692
693 static int
694 rip_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
695 {
696 return in_control(so, cmd, nam, ifp);
697 }
698
699 static int
700 rip_stat(struct socket *so, struct stat *ub)
701 {
702 KASSERT(solocked(so));
703
704 /* stat: don't bother with a blocksize. */
705 return 0;
706 }
707
708 static int
709 rip_peeraddr(struct socket *so, struct sockaddr *nam)
710 {
711 int s;
712
713 KASSERT(solocked(so));
714 KASSERT(sotoinpcb(so) != NULL);
715 KASSERT(nam != NULL);
716
717 s = splsoftnet();
718 in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
719 splx(s);
720
721 return 0;
722 }
723
724 static int
725 rip_sockaddr(struct socket *so, struct sockaddr *nam)
726 {
727 int s;
728
729 KASSERT(solocked(so));
730 KASSERT(sotoinpcb(so) != NULL);
731 KASSERT(nam != NULL);
732
733 s = splsoftnet();
734 in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
735 splx(s);
736
737 return 0;
738 }
739
740 static int
741 rip_rcvd(struct socket *so, int flags, struct lwp *l)
742 {
743 KASSERT(solocked(so));
744
745 return EOPNOTSUPP;
746 }
747
748 static int
749 rip_recvoob(struct socket *so, struct mbuf *m, int flags)
750 {
751 KASSERT(solocked(so));
752
753 return EOPNOTSUPP;
754 }
755
756 static int
757 rip_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
758 struct mbuf *control, struct lwp *l)
759 {
760 struct inpcb *inp = sotoinpcb(so);
761 int error = 0;
762 int s;
763
764 KASSERT(solocked(so));
765 KASSERT(inp != NULL);
766 KASSERT(m != NULL);
767
768 /*
769 * Ship a packet out. The appropriate raw output
770 * routine handles any massaging necessary.
771 */
772 s = splsoftnet();
773 if (nam) {
774 if ((so->so_state & SS_ISCONNECTED) != 0) {
775 error = EISCONN;
776 goto die;
777 }
778 error = rip_connect_pcb(inp, (struct sockaddr_in *)nam);
779 if (error)
780 goto die;
781 } else {
782 if ((so->so_state & SS_ISCONNECTED) == 0) {
783 error = ENOTCONN;
784 goto die;
785 }
786 }
787 error = rip_output(m, inp, control, l);
788 m = NULL;
789 control = NULL;
790 if (nam)
791 rip_disconnect1(inp);
792 die:
793 if (m != NULL)
794 m_freem(m);
795 if (control != NULL)
796 m_freem(control);
797
798 splx(s);
799 return error;
800 }
801
802 static int
803 rip_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
804 {
805 KASSERT(solocked(so));
806
807 m_freem(m);
808 m_freem(control);
809
810 return EOPNOTSUPP;
811 }
812
813 static int
814 rip_purgeif(struct socket *so, struct ifnet *ifp)
815 {
816 int s;
817
818 s = splsoftnet();
819 mutex_enter(softnet_lock);
820 in_pcbpurgeif0(&rawcbtable, ifp);
821 #ifdef NET_MPSAFE
822 mutex_exit(softnet_lock);
823 #endif
824 in_purgeif(ifp);
825 #ifdef NET_MPSAFE
826 mutex_enter(softnet_lock);
827 #endif
828 in_pcbpurgeif(&rawcbtable, ifp);
829 mutex_exit(softnet_lock);
830 splx(s);
831
832 return 0;
833 }
834
835 PR_WRAP_USRREQS(rip)
836 #define rip_attach rip_attach_wrapper
837 #define rip_detach rip_detach_wrapper
838 #define rip_accept rip_accept_wrapper
839 #define rip_bind rip_bind_wrapper
840 #define rip_listen rip_listen_wrapper
841 #define rip_connect rip_connect_wrapper
842 #define rip_connect2 rip_connect2_wrapper
843 #define rip_disconnect rip_disconnect_wrapper
844 #define rip_shutdown rip_shutdown_wrapper
845 #define rip_abort rip_abort_wrapper
846 #define rip_ioctl rip_ioctl_wrapper
847 #define rip_stat rip_stat_wrapper
848 #define rip_peeraddr rip_peeraddr_wrapper
849 #define rip_sockaddr rip_sockaddr_wrapper
850 #define rip_rcvd rip_rcvd_wrapper
851 #define rip_recvoob rip_recvoob_wrapper
852 #define rip_send rip_send_wrapper
853 #define rip_sendoob rip_sendoob_wrapper
854 #define rip_purgeif rip_purgeif_wrapper
855
856 const struct pr_usrreqs rip_usrreqs = {
857 .pr_attach = rip_attach,
858 .pr_detach = rip_detach,
859 .pr_accept = rip_accept,
860 .pr_bind = rip_bind,
861 .pr_listen = rip_listen,
862 .pr_connect = rip_connect,
863 .pr_connect2 = rip_connect2,
864 .pr_disconnect = rip_disconnect,
865 .pr_shutdown = rip_shutdown,
866 .pr_abort = rip_abort,
867 .pr_ioctl = rip_ioctl,
868 .pr_stat = rip_stat,
869 .pr_peeraddr = rip_peeraddr,
870 .pr_sockaddr = rip_sockaddr,
871 .pr_rcvd = rip_rcvd,
872 .pr_recvoob = rip_recvoob,
873 .pr_send = rip_send,
874 .pr_sendoob = rip_sendoob,
875 .pr_purgeif = rip_purgeif,
876 };
877
878 static void
879 sysctl_net_inet_raw_setup(struct sysctllog **clog)
880 {
881
882 sysctl_createv(clog, 0, NULL, NULL,
883 CTLFLAG_PERMANENT,
884 CTLTYPE_NODE, "inet", NULL,
885 NULL, 0, NULL, 0,
886 CTL_NET, PF_INET, CTL_EOL);
887 sysctl_createv(clog, 0, NULL, NULL,
888 CTLFLAG_PERMANENT,
889 CTLTYPE_NODE, "raw",
890 SYSCTL_DESCR("Raw IPv4 settings"),
891 NULL, 0, NULL, 0,
892 CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
893
894 sysctl_createv(clog, 0, NULL, NULL,
895 CTLFLAG_PERMANENT,
896 CTLTYPE_STRUCT, "pcblist",
897 SYSCTL_DESCR("Raw IPv4 control block list"),
898 sysctl_inpcblist, 0, &rawcbtable, 0,
899 CTL_NET, PF_INET, IPPROTO_RAW,
900 CTL_CREATE, CTL_EOL);
901 }
902