Annotation of src/sys/netinet/raw_ip.c, Revision 1.76
1.76 ! itojun 1: /* $NetBSD: raw_ip.c,v 1.75 2003/09/04 09:16:59 itojun Exp $ */
1.43 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.61 itojun 6: *
1.43 itojun 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.
1.61 itojun 18: *
1.43 itojun 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: */
1.14 cgd 31:
1.1 cgd 32: /*
1.13 mycroft 33: * Copyright (c) 1982, 1986, 1988, 1993
34: * The Regents of the University of California. All rights reserved.
1.1 cgd 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.
1.71 agc 44: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 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: *
1.39 thorpej 60: * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
1.1 cgd 61: */
1.59 lukem 62:
63: #include <sys/cdefs.h>
1.76 ! itojun 64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.75 2003/09/04 09:16:59 itojun Exp $");
1.40 scottr 65:
1.45 thorpej 66: #include "opt_ipsec.h"
1.40 scottr 67: #include "opt_mrouting.h"
1.1 cgd 68:
1.7 mycroft 69: #include <sys/param.h>
70: #include <sys/malloc.h>
71: #include <sys/mbuf.h>
72: #include <sys/socket.h>
73: #include <sys/protosw.h>
74: #include <sys/socketvar.h>
75: #include <sys/errno.h>
1.13 mycroft 76: #include <sys/systm.h>
1.26 mycroft 77: #include <sys/proc.h>
1.1 cgd 78:
1.7 mycroft 79: #include <net/if.h>
80: #include <net/route.h>
1.1 cgd 81:
1.7 mycroft 82: #include <netinet/in.h>
83: #include <netinet/in_systm.h>
84: #include <netinet/ip.h>
85: #include <netinet/ip_var.h>
1.13 mycroft 86: #include <netinet/ip_mroute.h>
1.44 darrenr 87: #include <netinet/ip_icmp.h>
1.7 mycroft 88: #include <netinet/in_pcb.h>
1.24 christos 89: #include <netinet/in_var.h>
90:
91: #include <machine/stdarg.h>
1.13 mycroft 92:
1.43 itojun 93: #ifdef IPSEC
94: #include <netinet6/ipsec.h>
95: #endif /*IPSEC*/
96:
1.72 jonathan 97: #ifdef FAST_IPSEC
98: #include <netipsec/ipsec.h>
99: #endif /* FAST_IPSEC*/
100:
1.20 mycroft 101: struct inpcbtable rawcbtable;
1.30 pk 102:
1.60 itojun 103: int rip_pcbnotify __P((struct inpcbtable *, struct in_addr,
104: struct in_addr, int, int, void (*) __P((struct inpcb *, int))));
1.30 pk 105: int rip_bind __P((struct inpcb *, struct mbuf *));
106: int rip_connect __P((struct inpcb *, struct mbuf *));
107: void rip_disconnect __P((struct inpcb *));
1.13 mycroft 108:
109: /*
110: * Nominal space allocated to a raw ip socket.
111: */
112: #define RIPSNDQ 8192
113: #define RIPRCVQ 8192
1.1 cgd 114:
115: /*
116: * Raw interface to IP protocol.
117: */
1.13 mycroft 118:
119: /*
120: * Initialize raw connection block q.
121: */
122: void
123: rip_init()
124: {
125:
1.33 mycroft 126: in_pcbinit(&rawcbtable, 1, 1);
1.13 mycroft 127: }
128:
1.1 cgd 129: /*
130: * Setup generic address and protocol structures
131: * for raw_input routine, then pass them along with
132: * mbuf chain.
133: */
1.9 mycroft 134: void
1.24 christos 135: #if __STDC__
136: rip_input(struct mbuf *m, ...)
137: #else
138: rip_input(m, va_alist)
1.1 cgd 139: struct mbuf *m;
1.24 christos 140: va_dcl
141: #endif
1.1 cgd 142: {
1.63 simonb 143: int proto;
1.53 augustss 144: struct ip *ip = mtod(m, struct ip *);
1.75 itojun 145: struct inpcb_hdr *inph;
1.53 augustss 146: struct inpcb *inp;
1.36 thorpej 147: struct inpcb *last = 0;
148: struct mbuf *opts = 0;
1.32 mycroft 149: struct sockaddr_in ripsrc;
1.43 itojun 150: va_list ap;
151:
152: va_start(ap, m);
1.64 simonb 153: (void)va_arg(ap, int); /* ignore value, advance ap */
1.43 itojun 154: proto = va_arg(ap, int);
155: va_end(ap);
1.1 cgd 156:
1.32 mycroft 157: ripsrc.sin_family = AF_INET;
158: ripsrc.sin_len = sizeof(struct sockaddr_in);
1.1 cgd 159: ripsrc.sin_addr = ip->ip_src;
1.34 mycroft 160: ripsrc.sin_port = 0;
161: bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
1.42 thorpej 162:
163: /*
164: * XXX Compatibility: programs using raw IP expect ip_len
1.62 itojun 165: * XXX to have the header length subtracted, and in host order.
166: * XXX ip_off is also expected to be host order.
1.42 thorpej 167: */
1.62 itojun 168: ip->ip_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
169: NTOHS(ip->ip_off);
1.32 mycroft 170:
1.75 itojun 171: CIRCLEQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
172: inp = (struct inpcb *)inph;
173: if (inp->inp_af != AF_INET)
174: continue;
1.43 itojun 175: if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
1.13 mycroft 176: continue;
1.32 mycroft 177: if (!in_nullhost(inp->inp_laddr) &&
178: !in_hosteq(inp->inp_laddr, ip->ip_dst))
1.13 mycroft 179: continue;
1.32 mycroft 180: if (!in_nullhost(inp->inp_faddr) &&
181: !in_hosteq(inp->inp_faddr, ip->ip_src))
1.13 mycroft 182: continue;
183: if (last) {
184: struct mbuf *n;
1.55 itojun 185:
1.72 jonathan 186: #if defined(IPSEC) || defined(FAST_IPSEC)
1.55 itojun 187: /* check AH/ESP integrity. */
188: if (ipsec4_in_reject_so(m, last->inp_socket)) {
189: ipsecstat.in_polvio++;
190: /* do not inject data to pcb */
191: } else
192: #endif /*IPSEC*/
1.24 christos 193: if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1.36 thorpej 194: if (last->inp_flags & INP_CONTROLOPTS ||
195: last->inp_socket->so_options & SO_TIMESTAMP)
196: ip_savecontrol(last, &opts, ip, n);
197: if (sbappendaddr(&last->inp_socket->so_rcv,
198: sintosa(&ripsrc), n, opts) == 0) {
1.13 mycroft 199: /* should notify about lost packet */
200: m_freem(n);
1.36 thorpej 201: if (opts)
202: m_freem(opts);
203: } else
204: sorwakeup(last->inp_socket);
1.41 thorpej 205: opts = NULL;
1.13 mycroft 206: }
207: }
1.36 thorpej 208: last = inp;
1.13 mycroft 209: }
1.72 jonathan 210: #if defined(IPSEC) || defined(FAST_IPSEC)
1.55 itojun 211: /* check AH/ESP integrity. */
212: if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
213: m_freem(m);
214: ipsecstat.in_polvio++;
215: ipstat.ips_delivered--;
216: /* do not inject data to pcb */
217: } else
218: #endif /*IPSEC*/
1.13 mycroft 219: if (last) {
1.36 thorpej 220: if (last->inp_flags & INP_CONTROLOPTS ||
221: last->inp_socket->so_options & SO_TIMESTAMP)
222: ip_savecontrol(last, &opts, ip, m);
223: if (sbappendaddr(&last->inp_socket->so_rcv,
224: sintosa(&ripsrc), m, opts) == 0) {
1.13 mycroft 225: m_freem(m);
1.36 thorpej 226: if (opts)
227: m_freem(opts);
228: } else
229: sorwakeup(last->inp_socket);
1.13 mycroft 230: } else {
1.44 darrenr 231: if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
1.47 itojun 232: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
233: 0, 0);
1.44 darrenr 234: ipstat.ips_noproto++;
235: ipstat.ips_delivered--;
236: } else
237: m_freem(m);
1.1 cgd 238: }
1.43 itojun 239: return;
1.60 itojun 240: }
241:
242: int
243: rip_pcbnotify(table, faddr, laddr, proto, errno, notify)
244: struct inpcbtable *table;
245: struct in_addr faddr, laddr;
246: int proto;
247: int errno;
248: void (*notify) __P((struct inpcb *, int));
249: {
250: struct inpcb *inp, *ninp;
251: int nmatch;
252:
253: nmatch = 0;
1.75 itojun 254: for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
1.60 itojun 255: inp != (struct inpcb *)&table->inpt_queue;
256: inp = ninp) {
1.75 itojun 257: ninp = (struct inpcb *)inp->inp_queue.cqe_next;
258: if (inp->inp_af != AF_INET)
259: continue;
1.60 itojun 260: if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
261: continue;
262: if (in_hosteq(inp->inp_faddr, faddr) &&
263: in_hosteq(inp->inp_laddr, laddr)) {
264: (*notify)(inp, errno);
265: nmatch++;
266: }
267: }
268:
269: return nmatch;
270: }
271:
272: void *
273: rip_ctlinput(cmd, sa, v)
274: int cmd;
275: struct sockaddr *sa;
276: void *v;
277: {
278: struct ip *ip = v;
279: void (*notify) __P((struct inpcb *, int)) = in_rtchange;
280: int errno;
281:
282: if (sa->sa_family != AF_INET ||
283: sa->sa_len != sizeof(struct sockaddr_in))
284: return NULL;
285: if ((unsigned)cmd >= PRC_NCMDS)
286: return NULL;
287: errno = inetctlerrmap[cmd];
288: if (PRC_IS_REDIRECT(cmd))
289: notify = in_rtchange, ip = 0;
290: else if (cmd == PRC_HOSTDEAD)
291: ip = 0;
292: else if (errno == 0)
293: return NULL;
294: if (ip) {
295: rip_pcbnotify(&rawcbtable, satosin(sa)->sin_addr,
296: ip->ip_src, ip->ip_p, errno, notify);
297:
298: /* XXX mapped address case */
299: } else
300: in_pcbnotifyall(&rawcbtable, satosin(sa)->sin_addr, errno,
301: notify);
302: return NULL;
1.1 cgd 303: }
304:
305: /*
306: * Generate IP header and pass packet to ip_output.
307: * Tack on options user may have setup with control call.
308: */
1.9 mycroft 309: int
1.24 christos 310: #if __STDC__
311: rip_output(struct mbuf *m, ...)
312: #else
313: rip_output(m, va_alist)
314: struct mbuf *m;
315: va_dcl
316: #endif
317: {
1.53 augustss 318: struct inpcb *inp;
319: struct ip *ip;
1.10 mycroft 320: struct mbuf *opts;
1.24 christos 321: int flags;
322: va_list ap;
323:
324: va_start(ap, m);
1.27 mycroft 325: inp = va_arg(ap, struct inpcb *);
1.24 christos 326: va_end(ap);
327:
1.27 mycroft 328: flags =
1.37 matt 329: (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
330: | IP_RETURNMTU;
1.1 cgd 331:
332: /*
333: * If the user handed us a complete IP packet, use it.
334: * Otherwise, allocate an mbuf for a header and fill it in.
335: */
1.13 mycroft 336: if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35 thorpej 337: if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
338: m_freem(m);
339: return (EMSGSIZE);
340: }
1.68 itojun 341: M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
342: if (!m)
343: return (ENOBUFS);
1.1 cgd 344: ip = mtod(m, struct ip *);
345: ip->ip_tos = 0;
1.62 itojun 346: ip->ip_off = htons(0);
1.13 mycroft 347: ip->ip_p = inp->inp_ip.ip_p;
1.62 itojun 348: ip->ip_len = htons(m->m_pkthdr.len);
1.13 mycroft 349: ip->ip_src = inp->inp_laddr;
1.27 mycroft 350: ip->ip_dst = inp->inp_faddr;
1.1 cgd 351: ip->ip_ttl = MAXTTL;
1.13 mycroft 352: opts = inp->inp_options;
353: } else {
1.35 thorpej 354: if (m->m_pkthdr.len > IP_MAXPACKET) {
355: m_freem(m);
356: return (EMSGSIZE);
357: }
1.13 mycroft 358: ip = mtod(m, struct ip *);
1.65 thorpej 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: return (ENOMEM); /* XXX */
371: ip = mtod(m, struct ip *);
372: }
373:
1.62 itojun 374: /* XXX userland passes ip_len and ip_off in host order */
1.38 mycroft 375: if (m->m_pkthdr.len != ip->ip_len) {
376: m_freem(m);
377: return (EINVAL);
378: }
1.62 itojun 379: HTONS(ip->ip_len);
380: HTONS(ip->ip_off);
1.13 mycroft 381: if (ip->ip_id == 0)
1.76 ! itojun 382: ip->ip_id = htons(ip_randomid());
1.13 mycroft 383: opts = NULL;
384: /* XXX prevent ip_output from overwriting header fields */
385: flags |= IP_RAWOUTPUT;
386: ipstat.ips_rawout++;
1.1 cgd 387: }
1.62 itojun 388: return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
1.73 itojun 389: inp->inp_socket, &inp->inp_errormtu));
1.1 cgd 390: }
391:
392: /*
393: * Raw IP socket option processing.
394: */
1.9 mycroft 395: int
1.1 cgd 396: rip_ctloutput(op, so, level, optname, m)
397: int op;
398: struct socket *so;
399: int level, optname;
400: struct mbuf **m;
401: {
1.53 augustss 402: struct inpcb *inp = sotoinpcb(so);
1.31 mycroft 403: int error = 0;
1.1 cgd 404:
1.15 mycroft 405: if (level != IPPROTO_IP) {
1.31 mycroft 406: error = ENOPROTOOPT;
407: if (op == PRCO_SETOPT && *m != 0)
408: (void) m_free(*m);
409: } else switch (op) {
410:
411: case PRCO_SETOPT:
412: switch (optname) {
413: case IP_HDRINCL:
414: if (*m == 0 || (*m)->m_len < sizeof (int))
415: error = EINVAL;
416: else {
1.13 mycroft 417: if (*mtod(*m, int *))
418: inp->inp_flags |= INP_HDRINCL;
419: else
420: inp->inp_flags &= ~INP_HDRINCL;
421: }
1.31 mycroft 422: if (*m != 0)
423: (void) m_free(*m);
424: break;
425:
426: #ifdef MROUTING
427: case MRT_INIT:
428: case MRT_DONE:
429: case MRT_ADD_VIF:
430: case MRT_DEL_VIF:
431: case MRT_ADD_MFC:
432: case MRT_DEL_MFC:
433: case MRT_ASSERT:
434: error = ip_mrouter_set(so, optname, m);
435: break;
436: #endif
437:
438: default:
439: error = ip_ctloutput(op, so, level, optname, m);
440: break;
1.13 mycroft 441: }
442: break;
1.1 cgd 443:
1.31 mycroft 444: case PRCO_GETOPT:
445: switch (optname) {
446: case IP_HDRINCL:
1.66 thorpej 447: *m = m_get(M_WAIT, MT_SOOPTS);
1.67 matt 448: MCLAIM((*m), so->so_mowner);
1.31 mycroft 449: (*m)->m_len = sizeof (int);
450: *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
451: break;
452:
1.6 hpeyerl 453: #ifdef MROUTING
1.31 mycroft 454: case MRT_VERSION:
455: case MRT_ASSERT:
456: error = ip_mrouter_get(so, optname, m);
1.18 mycroft 457: break;
1.31 mycroft 458: #endif
459:
1.18 mycroft 460: default:
1.31 mycroft 461: error = ip_ctloutput(op, so, level, optname, m);
1.18 mycroft 462: break;
463: }
1.31 mycroft 464: break;
1.1 cgd 465: }
1.31 mycroft 466: return (error);
1.1 cgd 467: }
468:
1.27 mycroft 469: int
1.29 mycroft 470: rip_bind(inp, nam)
471: struct inpcb *inp;
472: struct mbuf *nam;
473: {
474: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
475:
476: if (nam->m_len != sizeof(*addr))
477: return (EINVAL);
1.58 matt 478: if (TAILQ_FIRST(&ifnet) == 0)
1.29 mycroft 479: return (EADDRNOTAVAIL);
480: if (addr->sin_family != AF_INET &&
481: addr->sin_family != AF_IMPLINK)
482: return (EAFNOSUPPORT);
1.32 mycroft 483: if (!in_nullhost(addr->sin_addr) &&
1.29 mycroft 484: ifa_ifwithaddr(sintosa(addr)) == 0)
485: return (EADDRNOTAVAIL);
486: inp->inp_laddr = addr->sin_addr;
487: return (0);
488: }
489:
490: int
1.27 mycroft 491: rip_connect(inp, nam)
492: struct inpcb *inp;
493: struct mbuf *nam;
494: {
495: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
496:
497: if (nam->m_len != sizeof(*addr))
498: return (EINVAL);
1.58 matt 499: if (TAILQ_FIRST(&ifnet) == 0)
1.27 mycroft 500: return (EADDRNOTAVAIL);
501: if (addr->sin_family != AF_INET &&
502: addr->sin_family != AF_IMPLINK)
503: return (EAFNOSUPPORT);
504: inp->inp_faddr = addr->sin_addr;
505: return (0);
506: }
507:
508: void
509: rip_disconnect(inp)
510: struct inpcb *inp;
511: {
512:
1.32 mycroft 513: inp->inp_faddr = zeroin_addr;
1.27 mycroft 514: }
515:
1.13 mycroft 516: u_long rip_sendspace = RIPSNDQ;
517: u_long rip_recvspace = RIPRCVQ;
518:
1.1 cgd 519: /*ARGSUSED*/
1.9 mycroft 520: int
1.70 fvdl 521: rip_usrreq(so, req, m, nam, control, p)
1.53 augustss 522: struct socket *so;
1.1 cgd 523: int req;
1.2 cgd 524: struct mbuf *m, *nam, *control;
1.70 fvdl 525: struct proc *p;
1.1 cgd 526: {
1.53 augustss 527: struct inpcb *inp;
1.27 mycroft 528: int s;
1.53 augustss 529: int error = 0;
1.13 mycroft 530: #ifdef MROUTING
1.6 hpeyerl 531: extern struct socket *ip_mrouter;
532: #endif
1.27 mycroft 533:
1.22 pk 534: if (req == PRU_CONTROL)
535: return (in_control(so, (long)m, (caddr_t)nam,
1.26 mycroft 536: (struct ifnet *)control, p));
1.49 thorpej 537:
1.50 thorpej 538: if (req == PRU_PURGEIF) {
1.56 itojun 539: in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50 thorpej 540: in_purgeif((struct ifnet *)control);
541: in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.49 thorpej 542: return (0);
543: }
1.22 pk 544:
1.27 mycroft 545: s = splsoftnet();
546: inp = sotoinpcb(so);
1.28 mycroft 547: #ifdef DIAGNOSTIC
548: if (req != PRU_SEND && req != PRU_SENDOOB && control)
549: panic("rip_usrreq: unexpected control mbuf");
550: #endif
1.27 mycroft 551: if (inp == 0 && req != PRU_ATTACH) {
1.22 pk 552: error = EINVAL;
553: goto release;
554: }
555:
1.1 cgd 556: switch (req) {
557:
558: case PRU_ATTACH:
1.27 mycroft 559: if (inp != 0) {
560: error = EISCONN;
561: break;
562: }
1.26 mycroft 563: if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13 mycroft 564: error = EACCES;
565: break;
566: }
1.27 mycroft 567: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
568: error = soreserve(so, rip_sendspace, rip_recvspace);
569: if (error)
570: break;
571: }
572: error = in_pcballoc(so, &rawcbtable);
573: if (error)
1.13 mycroft 574: break;
1.27 mycroft 575: inp = sotoinpcb(so);
1.17 cgd 576: inp->inp_ip.ip_p = (long)nam;
1.1 cgd 577: break;
578:
579: case PRU_DETACH:
1.13 mycroft 580: #ifdef MROUTING
1.6 hpeyerl 581: if (so == ip_mrouter)
582: ip_mrouter_done();
583: #endif
1.13 mycroft 584: in_pcbdetach(inp);
1.1 cgd 585: break;
586:
587: case PRU_BIND:
1.29 mycroft 588: error = rip_bind(inp, nam);
1.27 mycroft 589: break;
590:
591: case PRU_LISTEN:
592: error = EOPNOTSUPP;
593: break;
594:
595: case PRU_CONNECT:
596: error = rip_connect(inp, nam);
597: if (error)
1.13 mycroft 598: break;
1.1 cgd 599: soisconnected(so);
1.13 mycroft 600: break;
601:
602: case PRU_CONNECT2:
603: error = EOPNOTSUPP;
604: break;
605:
1.27 mycroft 606: case PRU_DISCONNECT:
607: soisdisconnected(so);
608: rip_disconnect(inp);
609: break;
610:
1.13 mycroft 611: /*
612: * Mark the connection as being incapable of further input.
613: */
614: case PRU_SHUTDOWN:
615: socantsendmore(so);
616: break;
617:
1.27 mycroft 618: case PRU_RCVD:
619: error = EOPNOTSUPP;
620: break;
621:
1.13 mycroft 622: /*
623: * Ship a packet out. The appropriate raw output
624: * routine handles any massaging necessary.
625: */
626: case PRU_SEND:
1.28 mycroft 627: if (control && control->m_len) {
628: m_freem(control);
629: m_freem(m);
630: error = EINVAL;
631: break;
632: }
633: {
1.27 mycroft 634: if (nam) {
635: if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13 mycroft 636: error = EISCONN;
1.28 mycroft 637: goto die;
1.13 mycroft 638: }
1.27 mycroft 639: error = rip_connect(inp, nam);
640: if (error) {
1.28 mycroft 641: die:
1.27 mycroft 642: m_freem(m);
643: break;
644: }
1.13 mycroft 645: } else {
1.27 mycroft 646: if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13 mycroft 647: error = ENOTCONN;
1.28 mycroft 648: goto die;
1.13 mycroft 649: }
650: }
1.27 mycroft 651: error = rip_output(m, inp);
652: if (nam)
653: rip_disconnect(inp);
1.28 mycroft 654: }
1.13 mycroft 655: break;
656:
657: case PRU_SENSE:
658: /*
659: * stat: don't bother with a blocksize.
660: */
1.27 mycroft 661: splx(s);
1.1 cgd 662: return (0);
1.13 mycroft 663:
664: case PRU_RCVOOB:
1.27 mycroft 665: error = EOPNOTSUPP;
666: break;
667:
1.13 mycroft 668: case PRU_SENDOOB:
1.28 mycroft 669: m_freem(control);
1.27 mycroft 670: m_freem(m);
1.13 mycroft 671: error = EOPNOTSUPP;
672: break;
673:
674: case PRU_SOCKADDR:
675: in_setsockaddr(inp, nam);
676: break;
677:
678: case PRU_PEERADDR:
679: in_setpeeraddr(inp, nam);
680: break;
681:
682: default:
683: panic("rip_usrreq");
1.1 cgd 684: }
1.27 mycroft 685:
1.22 pk 686: release:
1.27 mycroft 687: splx(s);
1.1 cgd 688: return (error);
689: }
CVSweb <webmaster@jp.NetBSD.org>