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