source: rtems/cpukit/libnetworking/netinet/raw_ip.c @ c301570

4.104.114.84.95
Last change on this file since c301570 was c301570, checked in by Ralf Corsepius <ralf.corsepius@…>, on 05/10/07 at 05:12:54

Include <rtems/bsd/sys/queue.h> instead of <sys/queue.h>.

  • Property mode set to 100644
File size: 11.2 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
30 * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.147 2005/01/07 01:45:45 imp Exp $
31 */
32
33/*
34 *      $Id$
35 */
36 
37#include "opt_inet6.h"
38#include "opt_ipsec.h"
39#include "opt_mac.h"
40
41#include <sys/param.h>
42#include <rtems/bsd/sys/queue.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/protosw.h>
47#include <sys/socketvar.h>
48#include <sys/errno.h>
49#include <sys/systm.h>
50
51#include <net/if.h>
52#include <net/route.h>
53
54#define _IP_VHL
55#include <netinet/in.h>
56#include <netinet/in_systm.h>
57#include <netinet/in_pcb.h>
58#include <netinet/in_var.h>
59#include <netinet/ip.h>
60#include <netinet/ip_var.h>
61#include <netinet/ip_mroute.h>
62
63#include <netinet/ip_fw.h>
64
65#if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
66#undef COMPAT_IPFW
67#define COMPAT_IPFW 1
68#else
69#undef COMPAT_IPFW
70#endif
71
72static struct inpcbhead ripcb;
73static struct inpcbinfo ripcbinfo;
74
75/*
76 * Nominal space allocated to a raw ip socket.
77 */
78#define RIPSNDQ         8192
79#define RIPRCVQ         8192
80
81/*
82 * Raw interface to IP protocol.
83 */
84
85/*
86 * Initialize raw connection block q.
87 */
88void
89rip_init()
90{
91        LIST_INIT(&ripcb);
92        ripcbinfo.listhead = &ripcb;
93        /*
94         * XXX We don't use the hash list for raw IP, but it's easier
95         * to allocate a one entry hash list than it is to check all
96         * over the place for hashbase == NULL.
97         */
98        ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
99}
100
101static struct   sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
102/*
103 * Setup generic address and protocol structures
104 * for raw_input routine, then pass them along with
105 * mbuf chain.
106 */
107void
108rip_input(struct mbuf *m, int iphlen)
109{
110        struct ip *ip = mtod(m, struct ip *);
111        register struct inpcb *inp;
112        struct inpcb *last = 0;
113        struct mbuf *opts = 0;
114
115        ripsrc.sin_addr = ip->ip_src;
116        for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
117                if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p)
118                        continue;
119                if (inp->inp_laddr.s_addr &&
120                  inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
121                        continue;
122                if (inp->inp_faddr.s_addr &&
123                  inp->inp_faddr.s_addr != ip->ip_src.s_addr)
124                        continue;
125                if (last) {
126                        struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
127                        if (n) {
128                                if (last->inp_flags & INP_CONTROLOPTS ||
129                                    last->inp_socket->so_options & SO_TIMESTAMP)
130                                    ip_savecontrol(last, &opts, ip, n);
131                                if (sbappendaddr(&last->inp_socket->so_rcv,
132                                    (struct sockaddr *)&ripsrc, n,
133                                    opts) == 0) {
134                                        /* should notify about lost packet */
135                                        m_freem(n);
136                                        if (opts)
137                                            m_freem(opts);
138                                } else
139                                        sorwakeup(last->inp_socket);
140                                opts = 0;
141                        }
142                }
143                last = inp;
144        }
145        if (last) {
146                if (last->inp_flags & INP_CONTROLOPTS ||
147                    last->inp_socket->so_options & SO_TIMESTAMP)
148                        ip_savecontrol(last, &opts, ip, m);
149                if (sbappendaddr(&last->inp_socket->so_rcv,
150                    (struct sockaddr *)&ripsrc, m, opts) == 0) {
151                        m_freem(m);
152                        if (opts)
153                            m_freem(opts);
154                } else
155                        sorwakeup(last->inp_socket);
156        } else {
157                m_freem(m);
158              ipstat.ips_noproto++;
159              ipstat.ips_delivered--;
160      }
161}
162
163/*
164 * Generate IP header and pass packet to ip_output.
165 * Tack on options user may have setup with control call.
166 */
167int
168rip_output(struct mbuf *m, struct socket *so, u_long dst)
169{
170        struct ip *ip;
171        struct inpcb *inp = sotoinpcb(so);
172        int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
173            IP_ALLOWBROADCAST;
174
175        /*
176         * If the user handed us a complete IP packet, use it.
177         * Otherwise, allocate an mbuf for a header and fill it in.
178         */
179        if ((inp->inp_flags & INP_HDRINCL) == 0) {
180                if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
181                        m_freem(m);
182                        return(EMSGSIZE);
183                }
184                M_PREPEND(m, sizeof(struct ip), M_WAIT);
185                ip = mtod(m, struct ip *);
186                ip->ip_tos = 0;
187                ip->ip_off = 0;
188                ip->ip_p = inp->inp_ip_p;
189                ip->ip_len = m->m_pkthdr.len;
190                ip->ip_src = inp->inp_laddr;
191                ip->ip_dst.s_addr = dst;
192                ip->ip_ttl = MAXTTL;
193        } else {
194                if (m->m_pkthdr.len > IP_MAXPACKET) {
195                        m_freem(m);
196                        return(EMSGSIZE);
197                }
198                ip = mtod(m, struct ip *);
199                /* don't allow both user specified and setsockopt options,
200                   and don't allow packet length sizes that will crash */
201#ifdef _IP_VHL
202                if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
203                     && inp->inp_options)
204                    || (ip->ip_len > m->m_pkthdr.len)
205                    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
206#else
207                if (((ip->ip_hl != (sizeof (*ip) >> 2))
208                     && inp->inp_options)
209                    || (ip->ip_len > m->m_pkthdr.len)
210                    || (ip->ip_len < (ip->ip_hl << 2))) {
211#endif
212                        m_freem(m);
213                        return EINVAL;
214                }
215                if (ip->ip_id == 0)
216                        ip->ip_id = htons(ip_id++);
217                /* XXX prevent ip_output from overwriting header fields */
218                flags |= IP_RAWOUTPUT;
219                ipstat.ips_rawout++;
220        }
221        return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
222                          inp->inp_moptions));
223}
224
225/*
226 * Raw IP socket option processing.
227 */
228int
229rip_ctloutput(op, so, level, optname, m)
230        int op;
231        struct socket *so;
232        int level, optname;
233        struct mbuf **m;
234{
235        struct inpcb *inp = sotoinpcb(so);
236        int error;
237
238        if (level != IPPROTO_IP) {
239                if (op == PRCO_SETOPT && *m)
240                        (void)m_free(*m);
241                return (EINVAL);
242        }
243
244        switch (optname) {
245
246        case IP_HDRINCL:
247                error = 0;
248                if (op == PRCO_SETOPT) {
249                        if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
250                                error = EINVAL;
251                        else if (*mtod(*m, int *))
252                                inp->inp_flags |= INP_HDRINCL;
253                        else
254                                inp->inp_flags &= ~INP_HDRINCL;
255                        if (*m)
256                                (void)m_free(*m);
257                } else {
258                        *m = m_get(M_WAIT, MT_SOOPTS);
259                        (*m)->m_len = sizeof (int);
260                        *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
261                }
262                return (error);
263
264#ifdef COMPAT_IPFW
265        case IP_FW_GET:
266                if (ip_fw_ctl_ptr == NULL || op == PRCO_SETOPT) {
267                        if (*m) (void)m_free(*m);
268                        return(EINVAL);
269                }
270                return (*ip_fw_ctl_ptr)(optname, m);
271
272        case IP_FW_ADD:
273        case IP_FW_DEL:
274        case IP_FW_FLUSH:
275        case IP_FW_ZERO:
276                if (ip_fw_ctl_ptr == NULL || op != PRCO_SETOPT) {
277                        if (*m) (void)m_free(*m);
278                        return(EINVAL);
279                }
280                return (*ip_fw_ctl_ptr)(optname, m);
281
282        case IP_NAT:
283                if (ip_nat_ctl_ptr == NULL) {
284                        if (*m) (void)m_free(*m);
285                        return(EINVAL);
286                }
287                return (*ip_nat_ctl_ptr)(op, m);
288
289#endif
290        case IP_RSVP_ON:
291                return ip_rsvp_init(so);
292                break;
293
294        case IP_RSVP_OFF:
295                return ip_rsvp_done();
296                break;
297
298        case IP_RSVP_VIF_ON:
299                return ip_rsvp_vif_init(so, *m);
300
301        case IP_RSVP_VIF_OFF:
302                return ip_rsvp_vif_done(so, *m);
303
304        case MRT_INIT:
305        case MRT_DONE:
306        case MRT_ADD_VIF:
307        case MRT_DEL_VIF:
308        case MRT_ADD_MFC:
309        case MRT_DEL_MFC:
310        case MRT_VERSION:
311        case MRT_ASSERT:
312                if (op == PRCO_SETOPT) {
313                        error = ip_mrouter_set(optname, so, *m);
314                        if (*m)
315                                (void)m_free(*m);
316                } else if (op == PRCO_GETOPT) {
317                        error = ip_mrouter_get(optname, so, m);
318                } else
319                        error = EINVAL;
320                return (error);
321        }
322        return (ip_ctloutput(op, so, level, optname, m));
323}
324
325static u_long   rip_sendspace = RIPSNDQ; /* XXX sysctl ? */
326static u_long   rip_recvspace = RIPRCVQ; /* XXX sysctl ? */
327
328/*ARGSUSED*/
329int
330rip_usrreq(so, req, m, nam, control)
331        register struct socket *so;
332        int req;
333        struct mbuf *m, *nam, *control;
334{
335        register int error = 0;
336        register struct inpcb *inp = sotoinpcb(so);
337        int s;
338
339        if (req == PRU_CONTROL)
340                return (in_control(so, (u_long)m, (caddr_t)nam,
341                        (struct ifnet *)control));
342
343        switch (req) {
344
345        case PRU_ATTACH:
346                if (inp)
347                        panic("rip_attach");
348                if ((so->so_state & SS_PRIV) == 0) {
349                        error = EACCES;
350                        break;
351                }
352                s = splnet();
353                error = in_pcballoc(so, &ripcbinfo);
354                splx(s);
355                if (error)
356                        break;
357                error = soreserve(so, rip_sendspace, rip_recvspace);
358                if (error)
359                        break;
360                inp = (struct inpcb *)so->so_pcb;
361                inp->inp_ip_p = (int)nam;
362                break;
363
364        case PRU_DISCONNECT:
365                if ((so->so_state & SS_ISCONNECTED) == 0) {
366                        error = ENOTCONN;
367                        break;
368                }
369                /* FALLTHROUGH */
370        case PRU_ABORT:
371                soisdisconnected(so);
372                /* FALLTHROUGH */
373        case PRU_DETACH:
374                if (inp == 0)
375                        panic("rip_detach");
376                if (so == ip_mrouter)
377                        ip_mrouter_done();
378                ip_rsvp_force_done(so);
379                if (so == ip_rsvpd)
380                        ip_rsvp_done();
381                in_pcbdetach(inp);
382                break;
383
384        case PRU_BIND:
385            {
386                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
387
388                if (nam->m_len != sizeof(*addr)) {
389                        error = EINVAL;
390                        break;
391                }
392                if ((ifnet == 0) ||
393                    ((addr->sin_family != AF_INET) &&
394                     (addr->sin_family != AF_IMPLINK)) ||
395                    (addr->sin_addr.s_addr &&
396                     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
397                        error = EADDRNOTAVAIL;
398                        break;
399                }
400                inp->inp_laddr = addr->sin_addr;
401                break;
402            }
403        case PRU_CONNECT:
404            {
405                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
406
407                if (nam->m_len != sizeof(*addr)) {
408                        error = EINVAL;
409                        break;
410                }
411                if (ifnet == 0) {
412                        error = EADDRNOTAVAIL;
413                        break;
414                }
415                if ((addr->sin_family != AF_INET) &&
416                     (addr->sin_family != AF_IMPLINK)) {
417                        error = EAFNOSUPPORT;
418                        break;
419                }
420                inp->inp_faddr = addr->sin_addr;
421                soisconnected(so);
422                break;
423            }
424
425        case PRU_CONNECT2:
426                error = EOPNOTSUPP;
427                break;
428
429        /*
430         * Mark the connection as being incapable of further input.
431         */
432        case PRU_SHUTDOWN:
433                socantsendmore(so);
434                break;
435
436        /*
437         * Ship a packet out.  The appropriate raw output
438         * routine handles any massaging necessary.
439         */
440        case PRU_SEND:
441            {
442                register u_long dst;
443
444                if (so->so_state & SS_ISCONNECTED) {
445                        if (nam) {
446                                error = EISCONN;
447                                break;
448                        }
449                        dst = inp->inp_faddr.s_addr;
450                } else {
451                        if (nam == NULL) {
452                                error = ENOTCONN;
453                                break;
454                        }
455                        dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
456                }
457                error = rip_output(m, so, dst);
458                m = NULL;
459                break;
460            }
461
462        case PRU_SENSE:
463                /*
464                 * stat: don't bother with a blocksize.
465                 */
466                return (0);
467
468        /*
469         * Not supported.
470         */
471        case PRU_RCVOOB:
472        case PRU_RCVD:
473        case PRU_LISTEN:
474        case PRU_ACCEPT:
475        case PRU_SENDOOB:
476                error = EOPNOTSUPP;
477                break;
478
479        case PRU_SOCKADDR:
480                in_setsockaddr(inp, nam);
481                break;
482
483        case PRU_PEERADDR:
484                in_setpeeraddr(inp, nam);
485                break;
486
487        default:
488                panic("rip_usrreq");
489        }
490        if (m != NULL)
491                m_freem(m);
492        return (error);
493}
Note: See TracBrowser for help on using the repository browser.