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

4.104.115
Last change on this file since b3f8c9e1 was b3f8c9e1, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/22/08 at 07:47:28

Include <errno.h> (POSIX,C99) instead of <sys/errno.h> (BSD'ism).

  • Property mode set to 100644
File size: 11.2 KB
RevLine 
[39e6e65a]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
[4c92297a]30 * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.147 2005/01/07 01:45:45 imp Exp $
31 */
32
33/*
[39e6e65a]34 *      $Id$
35 */
[4c92297a]36 
37#include "opt_inet6.h"
38#include "opt_ipsec.h"
39#include "opt_mac.h"
[39e6e65a]40
41#include <sys/param.h>
[c301570]42#include <rtems/bsd/sys/queue.h>
[39e6e65a]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>
[b3f8c9e1]48#include <errno.h>
[39e6e65a]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>
[7ff51b00]59#include <netinet/ip.h>
[39e6e65a]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
[dd967330]89rip_init(void)
[39e6e65a]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
[4c92297a]108rip_input(struct mbuf *m, int iphlen)
[39e6e65a]109{
[4c92297a]110        struct ip *ip = mtod(m, struct ip *);
[39e6e65a]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
[4c92297a]168rip_output(struct mbuf *m, struct socket *so, u_long dst)
[39e6e65a]169{
[4c92297a]170        struct ip *ip;
171        struct inpcb *inp = sotoinpcb(so);
172        int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
173            IP_ALLOWBROADCAST;
[39e6e65a]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 */
[7ff51b00]201#ifdef _IP_VHL
[39e6e65a]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))) {
[7ff51b00]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
[39e6e65a]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
[dd967330]229rip_ctloutput(int op, struct socket *so, int level, int optname,
230        struct mbuf **m)
[39e6e65a]231{
[4c92297a]232        struct inpcb *inp = sotoinpcb(so);
233        int error;
[39e6e65a]234
235        if (level != IPPROTO_IP) {
236                if (op == PRCO_SETOPT && *m)
237                        (void)m_free(*m);
238                return (EINVAL);
239        }
240
241        switch (optname) {
242
243        case IP_HDRINCL:
244                error = 0;
245                if (op == PRCO_SETOPT) {
246                        if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
247                                error = EINVAL;
248                        else if (*mtod(*m, int *))
249                                inp->inp_flags |= INP_HDRINCL;
250                        else
251                                inp->inp_flags &= ~INP_HDRINCL;
252                        if (*m)
253                                (void)m_free(*m);
254                } else {
255                        *m = m_get(M_WAIT, MT_SOOPTS);
256                        (*m)->m_len = sizeof (int);
257                        *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
258                }
259                return (error);
260
261#ifdef COMPAT_IPFW
262        case IP_FW_GET:
263                if (ip_fw_ctl_ptr == NULL || op == PRCO_SETOPT) {
264                        if (*m) (void)m_free(*m);
265                        return(EINVAL);
266                }
267                return (*ip_fw_ctl_ptr)(optname, m);
268
269        case IP_FW_ADD:
270        case IP_FW_DEL:
271        case IP_FW_FLUSH:
272        case IP_FW_ZERO:
273                if (ip_fw_ctl_ptr == NULL || op != PRCO_SETOPT) {
274                        if (*m) (void)m_free(*m);
275                        return(EINVAL);
276                }
277                return (*ip_fw_ctl_ptr)(optname, m);
278
279        case IP_NAT:
280                if (ip_nat_ctl_ptr == NULL) {
281                        if (*m) (void)m_free(*m);
282                        return(EINVAL);
283                }
284                return (*ip_nat_ctl_ptr)(op, m);
285
286#endif
287        case IP_RSVP_ON:
288                return ip_rsvp_init(so);
289                break;
290
291        case IP_RSVP_OFF:
292                return ip_rsvp_done();
293                break;
294
295        case IP_RSVP_VIF_ON:
296                return ip_rsvp_vif_init(so, *m);
297
298        case IP_RSVP_VIF_OFF:
299                return ip_rsvp_vif_done(so, *m);
300
301        case MRT_INIT:
302        case MRT_DONE:
303        case MRT_ADD_VIF:
304        case MRT_DEL_VIF:
305        case MRT_ADD_MFC:
306        case MRT_DEL_MFC:
307        case MRT_VERSION:
308        case MRT_ASSERT:
309                if (op == PRCO_SETOPT) {
310                        error = ip_mrouter_set(optname, so, *m);
311                        if (*m)
312                                (void)m_free(*m);
313                } else if (op == PRCO_GETOPT) {
314                        error = ip_mrouter_get(optname, so, m);
315                } else
316                        error = EINVAL;
317                return (error);
318        }
319        return (ip_ctloutput(op, so, level, optname, m));
320}
321
322static u_long   rip_sendspace = RIPSNDQ; /* XXX sysctl ? */
323static u_long   rip_recvspace = RIPRCVQ; /* XXX sysctl ? */
324
325/*ARGSUSED*/
326int
[dd967330]327rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
328        struct mbuf *control)
[39e6e65a]329{
330        register int error = 0;
331        register struct inpcb *inp = sotoinpcb(so);
332        int s;
333
334        if (req == PRU_CONTROL)
335                return (in_control(so, (u_long)m, (caddr_t)nam,
336                        (struct ifnet *)control));
337
338        switch (req) {
339
340        case PRU_ATTACH:
341                if (inp)
342                        panic("rip_attach");
343                if ((so->so_state & SS_PRIV) == 0) {
344                        error = EACCES;
345                        break;
346                }
347                s = splnet();
348                error = in_pcballoc(so, &ripcbinfo);
349                splx(s);
350                if (error)
351                        break;
352                error = soreserve(so, rip_sendspace, rip_recvspace);
353                if (error)
354                        break;
355                inp = (struct inpcb *)so->so_pcb;
356                inp->inp_ip_p = (int)nam;
357                break;
358
359        case PRU_DISCONNECT:
360                if ((so->so_state & SS_ISCONNECTED) == 0) {
361                        error = ENOTCONN;
362                        break;
363                }
364                /* FALLTHROUGH */
365        case PRU_ABORT:
366                soisdisconnected(so);
367                /* FALLTHROUGH */
368        case PRU_DETACH:
369                if (inp == 0)
370                        panic("rip_detach");
371                if (so == ip_mrouter)
372                        ip_mrouter_done();
373                ip_rsvp_force_done(so);
374                if (so == ip_rsvpd)
375                        ip_rsvp_done();
376                in_pcbdetach(inp);
377                break;
378
379        case PRU_BIND:
380            {
381                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
382
383                if (nam->m_len != sizeof(*addr)) {
384                        error = EINVAL;
385                        break;
386                }
387                if ((ifnet == 0) ||
388                    ((addr->sin_family != AF_INET) &&
389                     (addr->sin_family != AF_IMPLINK)) ||
390                    (addr->sin_addr.s_addr &&
391                     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
392                        error = EADDRNOTAVAIL;
393                        break;
394                }
395                inp->inp_laddr = addr->sin_addr;
396                break;
397            }
398        case PRU_CONNECT:
399            {
400                struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
401
402                if (nam->m_len != sizeof(*addr)) {
403                        error = EINVAL;
404                        break;
405                }
406                if (ifnet == 0) {
407                        error = EADDRNOTAVAIL;
408                        break;
409                }
410                if ((addr->sin_family != AF_INET) &&
411                     (addr->sin_family != AF_IMPLINK)) {
412                        error = EAFNOSUPPORT;
413                        break;
414                }
415                inp->inp_faddr = addr->sin_addr;
416                soisconnected(so);
417                break;
418            }
419
420        case PRU_CONNECT2:
421                error = EOPNOTSUPP;
422                break;
423
424        /*
425         * Mark the connection as being incapable of further input.
426         */
427        case PRU_SHUTDOWN:
428                socantsendmore(so);
429                break;
430
431        /*
432         * Ship a packet out.  The appropriate raw output
433         * routine handles any massaging necessary.
434         */
435        case PRU_SEND:
436            {
437                register u_long dst;
438
439                if (so->so_state & SS_ISCONNECTED) {
440                        if (nam) {
441                                error = EISCONN;
442                                break;
443                        }
444                        dst = inp->inp_faddr.s_addr;
445                } else {
446                        if (nam == NULL) {
447                                error = ENOTCONN;
448                                break;
449                        }
450                        dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
451                }
452                error = rip_output(m, so, dst);
453                m = NULL;
454                break;
455            }
456
457        case PRU_SENSE:
458                /*
459                 * stat: don't bother with a blocksize.
460                 */
461                return (0);
462
463        /*
464         * Not supported.
465         */
466        case PRU_RCVOOB:
467        case PRU_RCVD:
468        case PRU_LISTEN:
469        case PRU_ACCEPT:
470        case PRU_SENDOOB:
471                error = EOPNOTSUPP;
472                break;
473
474        case PRU_SOCKADDR:
475                in_setsockaddr(inp, nam);
476                break;
477
478        case PRU_PEERADDR:
479                in_setpeeraddr(inp, nam);
480                break;
481
482        default:
483                panic("rip_usrreq");
484        }
485        if (m != NULL)
486                m_freem(m);
487        return (error);
488}
Note: See TracBrowser for help on using the repository browser.