2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
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,
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
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
56 * @(#)raw_ip.c 7.8 (Berkeley) 7/25/90
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.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>
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
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
;
130 so
= va_arg(va
, struct socket
*);
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
*);
141 M_PREPEND(m
, sizeof(struct ip
), M_WAIT
);
142 ip
= mtod(m
, struct ip
*);
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
;
150 ip
->ip_src
.s_addr
= 0;
151 if (sin
= satosin(rp
->rinp_rcb
.rcb_faddr
))
152 ip
->ip_dst
= sin
->sin_addr
;
156 (rp
->rinp_flags
& RINPF_HDRINCL
)? (struct mbuf
*)0: rp
->rinp_options
,
158 (so
->so_options
& SO_DONTROUTE
) | IP_ALLOWBROADCAST
));
162 * Raw IP socket option processing.
165 rip_ctloutput(op
, so
, level
, optname
, m
)
172 register struct raw_inpcb
*rp
= sotorawinpcb(so
);
174 if (level
!= IPPROTO_IP
)
182 return (ip_pcbopts(&rp
->rinp_options
, *m
));
185 if (m
== 0 || *m
== 0 || (*m
)->m_len
< sizeof (int)) {
189 if (*mtod(*m
, int *))
190 rp
->rinp_flags
|= RINPF_HDRINCL
;
192 rp
->rinp_flags
&= ~RINPF_HDRINCL
;
202 *m
= m_get(M_WAIT
, MT_SOOPTS
);
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
);
215 (*m
)->m_len
= sizeof (int);
216 *mtod(*m
, int *) = rp
->rinp_flags
& RINPF_HDRINCL
;
227 if (op
== PRCO_SETOPT
&& *m
)
233 rip_usrreq(so
, req
, m
, nam
, /*rights,*/ control
)
234 register struct socket
*so
;
236 struct mbuf
*m
, *nam
, /**rights,*/ *control
;
238 register int error
= 0;
239 register struct raw_inpcb
*rp
= sotorawinpcb(so
);
246 MALLOC(rp
, struct raw_inpcb
*, sizeof *rp
, M_PCB
, M_WAITOK
);
249 aligned_bzero_const((caddr_t
)rp
, sizeof(*rp
));
250 so
->so_pcb
= (caddr_t
)rp
;
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;
266 struct sockaddr_in
*addr
= mtod(nam
, struct sockaddr_in
*);
268 if (nam
->m_len
!= sizeof(*addr
))
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
;
282 struct sockaddr_in
*addr
= mtod(nam
, struct sockaddr_in
*);
284 if (nam
->m_len
!= sizeof(*addr
))
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
;
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
);