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