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