Annotation of src/sys/netinet/raw_ip.c, Revision 1.88.4.1
1.88.4.1! rpaulo 1: /* $NetBSD: raw_ip.c,v 1.88 2005/12/11 12:24:57 christos 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.88.4.1! rpaulo 64: __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.88 2005/12/11 12:24:57 christos 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>
1.84 atatat 71: #include <sys/sysctl.h>
1.7 mycroft 72: #include <sys/malloc.h>
73: #include <sys/mbuf.h>
74: #include <sys/socket.h>
75: #include <sys/protosw.h>
76: #include <sys/socketvar.h>
77: #include <sys/errno.h>
1.13 mycroft 78: #include <sys/systm.h>
1.26 mycroft 79: #include <sys/proc.h>
1.1 cgd 80:
1.7 mycroft 81: #include <net/if.h>
82: #include <net/route.h>
1.1 cgd 83:
1.7 mycroft 84: #include <netinet/in.h>
85: #include <netinet/in_systm.h>
86: #include <netinet/ip.h>
87: #include <netinet/ip_var.h>
1.13 mycroft 88: #include <netinet/ip_mroute.h>
1.44 darrenr 89: #include <netinet/ip_icmp.h>
1.7 mycroft 90: #include <netinet/in_pcb.h>
1.87 yamt 91: #include <netinet/in_proto.h>
1.24 christos 92: #include <netinet/in_var.h>
93:
94: #include <machine/stdarg.h>
1.13 mycroft 95:
1.43 itojun 96: #ifdef IPSEC
97: #include <netinet6/ipsec.h>
98: #endif /*IPSEC*/
99:
1.72 jonathan 100: #ifdef FAST_IPSEC
101: #include <netipsec/ipsec.h>
1.80 jonathan 102: #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
1.72 jonathan 103: #endif /* FAST_IPSEC*/
104:
1.20 mycroft 105: struct inpcbtable rawcbtable;
1.30 pk 106:
1.82 perry 107: int rip_pcbnotify(struct inpcbtable *, struct in_addr,
108: struct in_addr, int, int, void (*)(struct inpcb *, int));
109: int rip_bind(struct inpcb *, struct mbuf *);
110: int rip_connect(struct inpcb *, struct mbuf *);
111: void rip_disconnect(struct inpcb *);
1.13 mycroft 112:
113: /*
114: * Nominal space allocated to a raw ip socket.
115: */
116: #define RIPSNDQ 8192
117: #define RIPRCVQ 8192
1.1 cgd 118:
119: /*
120: * Raw interface to IP protocol.
121: */
1.13 mycroft 122:
123: /*
124: * Initialize raw connection block q.
125: */
126: void
1.83 perry 127: rip_init(void)
1.13 mycroft 128: {
129:
1.33 mycroft 130: in_pcbinit(&rawcbtable, 1, 1);
1.13 mycroft 131: }
132:
1.1 cgd 133: /*
134: * Setup generic address and protocol structures
135: * for raw_input routine, then pass them along with
136: * mbuf chain.
137: */
1.9 mycroft 138: void
1.24 christos 139: rip_input(struct mbuf *m, ...)
1.1 cgd 140: {
1.63 simonb 141: int proto;
1.53 augustss 142: struct ip *ip = mtod(m, struct ip *);
143: struct inpcb *inp;
1.36 thorpej 144: struct inpcb *last = 0;
145: struct mbuf *opts = 0;
1.32 mycroft 146: struct sockaddr_in ripsrc;
1.43 itojun 147: va_list ap;
148:
149: va_start(ap, m);
1.64 simonb 150: (void)va_arg(ap, int); /* ignore value, advance ap */
1.43 itojun 151: proto = va_arg(ap, int);
152: va_end(ap);
1.1 cgd 153:
1.32 mycroft 154: ripsrc.sin_family = AF_INET;
155: ripsrc.sin_len = sizeof(struct sockaddr_in);
1.1 cgd 156: ripsrc.sin_addr = ip->ip_src;
1.34 mycroft 157: ripsrc.sin_port = 0;
158: bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
1.42 thorpej 159:
160: /*
161: * XXX Compatibility: programs using raw IP expect ip_len
1.62 itojun 162: * XXX to have the header length subtracted, and in host order.
163: * XXX ip_off is also expected to be host order.
1.42 thorpej 164: */
1.62 itojun 165: ip->ip_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
166: NTOHS(ip->ip_off);
1.32 mycroft 167:
1.88.4.1! rpaulo 168: CIRCLEQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) {
1.75 itojun 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,
1.88 christos 503: struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
1.1 cgd 504: {
1.53 augustss 505: struct inpcb *inp;
1.88 christos 506: struct proc *p;
1.27 mycroft 507: int s;
1.53 augustss 508: int error = 0;
1.13 mycroft 509: #ifdef MROUTING
1.6 hpeyerl 510: extern struct socket *ip_mrouter;
511: #endif
1.27 mycroft 512:
1.88 christos 513: p = l ? l->l_proc : NULL;
1.22 pk 514: if (req == PRU_CONTROL)
515: return (in_control(so, (long)m, (caddr_t)nam,
1.26 mycroft 516: (struct ifnet *)control, p));
1.49 thorpej 517:
1.50 thorpej 518: if (req == PRU_PURGEIF) {
1.56 itojun 519: in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
1.50 thorpej 520: in_purgeif((struct ifnet *)control);
521: in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
1.49 thorpej 522: return (0);
523: }
1.22 pk 524:
1.27 mycroft 525: s = splsoftnet();
526: inp = sotoinpcb(so);
1.28 mycroft 527: #ifdef DIAGNOSTIC
528: if (req != PRU_SEND && req != PRU_SENDOOB && control)
529: panic("rip_usrreq: unexpected control mbuf");
530: #endif
1.27 mycroft 531: if (inp == 0 && req != PRU_ATTACH) {
1.22 pk 532: error = EINVAL;
533: goto release;
534: }
535:
1.1 cgd 536: switch (req) {
537:
538: case PRU_ATTACH:
1.27 mycroft 539: if (inp != 0) {
540: error = EISCONN;
541: break;
542: }
1.26 mycroft 543: if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13 mycroft 544: error = EACCES;
545: break;
546: }
1.27 mycroft 547: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
548: error = soreserve(so, rip_sendspace, rip_recvspace);
549: if (error)
550: break;
551: }
552: error = in_pcballoc(so, &rawcbtable);
553: if (error)
1.13 mycroft 554: break;
1.27 mycroft 555: inp = sotoinpcb(so);
1.17 cgd 556: inp->inp_ip.ip_p = (long)nam;
1.1 cgd 557: break;
558:
559: case PRU_DETACH:
1.13 mycroft 560: #ifdef MROUTING
1.6 hpeyerl 561: if (so == ip_mrouter)
562: ip_mrouter_done();
563: #endif
1.13 mycroft 564: in_pcbdetach(inp);
1.1 cgd 565: break;
566:
567: case PRU_BIND:
1.29 mycroft 568: error = rip_bind(inp, nam);
1.27 mycroft 569: break;
570:
571: case PRU_LISTEN:
572: error = EOPNOTSUPP;
573: break;
574:
575: case PRU_CONNECT:
576: error = rip_connect(inp, nam);
577: if (error)
1.13 mycroft 578: break;
1.1 cgd 579: soisconnected(so);
1.13 mycroft 580: break;
581:
582: case PRU_CONNECT2:
583: error = EOPNOTSUPP;
584: break;
585:
1.27 mycroft 586: case PRU_DISCONNECT:
587: soisdisconnected(so);
588: rip_disconnect(inp);
589: break;
590:
1.13 mycroft 591: /*
592: * Mark the connection as being incapable of further input.
593: */
594: case PRU_SHUTDOWN:
595: socantsendmore(so);
596: break;
597:
1.27 mycroft 598: case PRU_RCVD:
599: error = EOPNOTSUPP;
600: break;
601:
1.13 mycroft 602: /*
603: * Ship a packet out. The appropriate raw output
604: * routine handles any massaging necessary.
605: */
606: case PRU_SEND:
1.28 mycroft 607: if (control && control->m_len) {
608: m_freem(control);
609: m_freem(m);
610: error = EINVAL;
611: break;
612: }
613: {
1.27 mycroft 614: if (nam) {
615: if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13 mycroft 616: error = EISCONN;
1.28 mycroft 617: goto die;
1.13 mycroft 618: }
1.27 mycroft 619: error = rip_connect(inp, nam);
620: if (error) {
1.28 mycroft 621: die:
1.27 mycroft 622: m_freem(m);
623: break;
624: }
1.13 mycroft 625: } else {
1.27 mycroft 626: if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13 mycroft 627: error = ENOTCONN;
1.28 mycroft 628: goto die;
1.13 mycroft 629: }
630: }
1.27 mycroft 631: error = rip_output(m, inp);
632: if (nam)
633: rip_disconnect(inp);
1.28 mycroft 634: }
1.13 mycroft 635: break;
636:
637: case PRU_SENSE:
638: /*
639: * stat: don't bother with a blocksize.
640: */
1.27 mycroft 641: splx(s);
1.1 cgd 642: return (0);
1.13 mycroft 643:
644: case PRU_RCVOOB:
1.27 mycroft 645: error = EOPNOTSUPP;
646: break;
647:
1.13 mycroft 648: case PRU_SENDOOB:
1.28 mycroft 649: m_freem(control);
1.27 mycroft 650: m_freem(m);
1.13 mycroft 651: error = EOPNOTSUPP;
652: break;
653:
654: case PRU_SOCKADDR:
655: in_setsockaddr(inp, nam);
656: break;
657:
658: case PRU_PEERADDR:
659: in_setpeeraddr(inp, nam);
660: break;
661:
662: default:
663: panic("rip_usrreq");
1.1 cgd 664: }
1.27 mycroft 665:
1.22 pk 666: release:
1.27 mycroft 667: splx(s);
1.1 cgd 668: return (error);
669: }
1.84 atatat 670:
671: SYSCTL_SETUP(sysctl_net_inet_raw_setup, "sysctl net.inet.raw subtree setup")
672: {
673:
674: sysctl_createv(clog, 0, NULL, NULL,
675: CTLFLAG_PERMANENT,
676: CTLTYPE_NODE, "net", NULL,
677: NULL, 0, NULL, 0,
678: CTL_NET, CTL_EOL);
679: sysctl_createv(clog, 0, NULL, NULL,
680: CTLFLAG_PERMANENT,
681: CTLTYPE_NODE, "inet", NULL,
682: NULL, 0, NULL, 0,
683: CTL_NET, PF_INET, CTL_EOL);
684: sysctl_createv(clog, 0, NULL, NULL,
685: CTLFLAG_PERMANENT,
686: CTLTYPE_NODE, "raw",
687: SYSCTL_DESCR("Raw IPv4 settings"),
688: NULL, 0, NULL, 0,
689: CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
690:
691: sysctl_createv(clog, 0, NULL, NULL,
692: CTLFLAG_PERMANENT,
1.86 atatat 693: CTLTYPE_STRUCT, "pcblist",
1.84 atatat 694: SYSCTL_DESCR("Raw IPv4 control block list"),
695: sysctl_inpcblist, 0, &rawcbtable, 0,
696: CTL_NET, PF_INET, IPPROTO_RAW,
697: CTL_CREATE, CTL_EOL);
698: }
CVSweb <webmaster@jp.NetBSD.org>