tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / netinet / raw_ip.c
blob23ab99e511a8b33cd06648cdae68c34ad78a98c4
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 Neil Cafferkey
6 * Copyright (C) 2005 Pavel Fedin
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA.
25 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
56 * @(#)raw_ip.c 7.8 (Berkeley) 7/25/90
59 #include <conf.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
64 #include <sys/mbuf.h>
65 #include <sys/socket.h>
66 #include <sys/protosw.h>
67 #include <sys/socketvar.h>
68 #include <sys/errno.h>
69 #include <sys/synch.h>
71 #include <net/if.h>
72 #include <net/route.h>
73 #include <net/raw_cb.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/in_pcb.h>
81 #include <netinet/raw_ip_protos.h>
82 #include <netinet/ip_output_protos.h>
83 #include <net/raw_usrreq_protos.h>
84 #include <kern/uipc_socket2_protos.h>
86 extern struct ifnet *ifnet;
89 * Raw interface to IP protocol.
92 struct sockaddr_in ripdst = { sizeof(ripdst), AF_INET };
93 struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
94 struct sockproto ripproto = { PF_INET };
96 * Setup generic address and protocol structures
97 * for raw_input routine, then pass them along with
98 * mbuf chain.
100 void rip_input(void *arg, ...)
102 struct mbuf *m = arg;
103 register struct ip *ip = mtod(m, struct ip *);
105 ripproto.sp_protocol = ip->ip_p;
106 ripdst.sin_addr = ip->ip_dst;
107 ripsrc.sin_addr = ip->ip_src;
108 if (raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
109 (struct sockaddr *)&ripdst) == 0) {
110 ipstat.ips_noproto++;
111 ipstat.ips_delivered--;
116 * Generate IP header and pass packet to ip_output.
117 * Tack on options user may have setup with control call.
119 #define satosin(sa) ((struct sockaddr_in *)(sa))
120 int rip_output(void *arg, ...)
122 register struct ip *ip;
123 register struct raw_inpcb *rp;
124 register struct sockaddr_in *sin;
125 register struct mbuf *m = arg;
126 struct socket *so;
127 va_list va;
129 va_start(va, arg);
130 so = va_arg(va, struct socket *);
131 va_end(va);
133 rp = sotorawinpcb(so);
135 * If the user handed us a complete IP packet, use it.
136 * Otherwise, allocate an mbuf for a header and fill it in.
138 if (rp->rinp_flags & RINPF_HDRINCL)
139 ip = mtod(m, struct ip *);
140 else {
141 M_PREPEND(m, sizeof(struct ip), M_WAIT);
142 ip = mtod(m, struct ip *);
143 ip->ip_tos = 0;
144 ip->ip_off = 0;
145 ip->ip_p = rp->rinp_rcb.rcb_proto.sp_protocol;
146 ip->ip_len = m->m_pkthdr.len;
147 if (sin = satosin(rp->rinp_rcb.rcb_laddr)) {
148 ip->ip_src = sin->sin_addr;
149 } else
150 ip->ip_src.s_addr = 0;
151 if (sin = satosin(rp->rinp_rcb.rcb_faddr))
152 ip->ip_dst = sin->sin_addr;
153 ip->ip_ttl = MAXTTL;
155 return (ip_output(m,
156 (rp->rinp_flags & RINPF_HDRINCL)? (struct mbuf *)0: rp->rinp_options,
157 &rp->rinp_route,
158 (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST));
162 * Raw IP socket option processing.
165 rip_ctloutput(op, so, level, optname, m)
166 int op;
167 struct socket *so;
168 int level, optname;
169 struct mbuf **m;
171 int error = 0;
172 register struct raw_inpcb *rp = sotorawinpcb(so);
174 if (level != IPPROTO_IP)
175 error = EINVAL;
176 else switch (op) {
178 case PRCO_SETOPT:
179 switch (optname) {
181 case IP_OPTIONS:
182 return (ip_pcbopts(&rp->rinp_options, *m));
184 case IP_HDRINCL:
185 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) {
186 error = EINVAL;
187 break;
189 if (*mtod(*m, int *))
190 rp->rinp_flags |= RINPF_HDRINCL;
191 else
192 rp->rinp_flags &= ~RINPF_HDRINCL;
193 break;
195 default:
196 error = EINVAL;
197 break;
199 break;
201 case PRCO_GETOPT:
202 *m = m_get(M_WAIT, MT_SOOPTS);
203 switch (optname) {
205 case IP_OPTIONS:
206 if (rp->rinp_options) {
207 (*m)->m_len = rp->rinp_options->m_len;
208 aligned_bcopy(mtod(rp->rinp_options, caddr_t),
209 mtod(*m, caddr_t), (unsigned)(*m)->m_len);
210 } else
211 (*m)->m_len = 0;
212 break;
214 case IP_HDRINCL:
215 (*m)->m_len = sizeof (int);
216 *mtod(*m, int *) = rp->rinp_flags & RINPF_HDRINCL;
217 break;
219 default:
220 error = EINVAL;
221 m_freem(*m);
222 *m = 0;
223 break;
225 break;
227 if (op == PRCO_SETOPT && *m)
228 (void)m_free(*m);
229 return (error);
233 rip_usrreq(so, req, m, nam, /*rights,*/ control)
234 register struct socket *so;
235 int req;
236 struct mbuf *m, *nam, /**rights,*/ *control;
238 register int error = 0;
239 register struct raw_inpcb *rp = sotorawinpcb(so);
241 switch (req) {
243 case PRU_ATTACH:
244 if (rp)
245 panic("rip_attach");
246 MALLOC(rp, struct raw_inpcb *, sizeof *rp, M_PCB, M_WAITOK);
247 if (rp == 0)
248 return (ENOBUFS);
249 aligned_bzero_const((caddr_t)rp, sizeof(*rp));
250 so->so_pcb = (caddr_t)rp;
251 break;
253 case PRU_DETACH:
254 if (rp == 0)
255 panic("rip_detach");
256 if (rp->rinp_options)
257 m_freem(rp->rinp_options);
258 if (rp->rinp_route.ro_rt)
259 RTFREE(rp->rinp_route.ro_rt);
260 if (rp->rinp_rcb.rcb_laddr)
261 rp->rinp_rcb.rcb_laddr = 0;
262 break;
264 case PRU_BIND:
266 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
268 if (nam->m_len != sizeof(*addr))
269 return (EINVAL);
270 if ((ifnet == 0) ||
271 ((addr->sin_family != AF_INET) &&
272 (addr->sin_family != AF_IMPLINK)) ||
273 (addr->sin_addr.s_addr &&
274 ifa_ifwithaddr((struct sockaddr *)addr) == 0))
275 return (EADDRNOTAVAIL);
276 rp->rinp_rcb.rcb_laddr = (struct sockaddr *)&rp->rinp_laddr;
277 rp->rinp_laddr = *addr;
278 return (0);
280 case PRU_CONNECT:
282 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
284 if (nam->m_len != sizeof(*addr))
285 return (EINVAL);
286 if (ifnet == 0)
287 return (EADDRNOTAVAIL);
288 if ((addr->sin_family != AF_INET) &&
289 (addr->sin_family != AF_IMPLINK))
290 return (EAFNOSUPPORT);
291 rp->rinp_rcb.rcb_faddr = (struct sockaddr *)&rp->rinp_faddr;
292 rp->rinp_faddr = *addr;
293 soisconnected(so);
294 return (0);
297 error = raw_usrreq(so, req, m, nam, /*rights,*/ control);
299 if (error && (req == PRU_ATTACH) && so->so_pcb)
300 bsd_free(so->so_pcb, M_PCB);
301 return (error);