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