clean up some icmp stuff; add EHOSTDOWN & EHOSTUNREACH
[unix-history] / usr / src / sys / netinet / raw_ip.c
CommitLineData
02122dcc 1/* raw_ip.c 4.11 82/04/11 */
2a598b25
BJ
2
3#include "../h/param.h"
4#include "../h/mbuf.h"
5#include "../h/socket.h"
94a62155 6#include "../h/protosw.h"
2a598b25 7#include "../h/socketvar.h"
126472ab 8#include "../net/if.h"
2a598b25
BJ
9#include "../net/in.h"
10#include "../net/in_systm.h"
94a62155
BJ
11#include "../net/ip.h"
12#include "../net/ip_var.h"
13#include "../net/raw_cb.h"
126472ab 14#include <errno.h>
2a598b25
BJ
15
16/*
94a62155
BJ
17 * Raw interface to IP protocol.
18 */
19
ee787340
SL
20static struct sockaddr_in ripdst = { AF_INET };
21static struct sockaddr_in ripsrc = { AF_INET };
22static struct sockproto ripproto = { PF_INET };
94a62155
BJ
23/*
24 * Setup generic address and protocol structures
25 * for raw_input routine, then pass them along with
26 * mbuf chain.
2a598b25 27 */
2a598b25
BJ
28rip_input(m)
29 struct mbuf *m;
30{
94a62155 31 register struct ip *ip = mtod(m, struct ip *);
2a598b25
BJ
32
33COUNT(RIP_INPUT);
94a62155 34 ripproto.sp_protocol = ip->ip_p;
faad37c0
SL
35 ripdst.sin_addr = ip->ip_dst;
36 ripsrc.sin_addr = ip->ip_src;
02122dcc
SL
37 raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
38 (struct sockaddr *)&ripdst);
2a598b25
BJ
39}
40
94a62155
BJ
41/*
42 * Generate IP header and pass packet to ip_output.
43 * Tack on options user may have setup with control call.
44 */
45rip_output(m0, so)
46 struct mbuf *m0;
47 struct socket *so;
2a598b25 48{
94a62155
BJ
49 register struct mbuf *m;
50 register struct ip *ip;
126472ab
SL
51 int len = 0, error;
52 struct rawcb *rp = sotorawcb(so);
53 struct ifnet *ifp;
54 struct sockaddr_in *src;
2a598b25
BJ
55
56COUNT(RIP_OUTPUT);
94a62155
BJ
57 /*
58 * Calculate data length and get an mbuf
59 * for IP header.
60 */
61 for (m = m0; m; m = m->m_next)
62 len += m->m_len;
63 m = m_get(M_DONTWAIT);
64 if (m == 0) {
126472ab
SL
65 error = ENOBUFS;
66 goto bad;
94a62155
BJ
67 }
68
69 /*
70 * Fill in IP header as needed.
71 */
72 m->m_off = MMAXOFF - sizeof(struct ip);
73 m->m_len = sizeof(struct ip);
74 m->m_next = m0;
75 ip = mtod(m, struct ip *);
76 ip->ip_p = so->so_proto->pr_protocol;
77 ip->ip_len = sizeof(struct ip) + len;
126472ab
SL
78 ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
79 /* local address may not be specified -- XXX */
80 ifp = if_ifonnetof(ip->ip_dst.s_net);
81 if (ifp == 0) {
82 error = ENETUNREACH;
83 goto bad;
84 }
85 if (rp->rcb_flags & RAW_LADDR)
86 src = (struct sockaddr_in *)&rp->rcb_laddr;
87 else {
88 if (ifp->if_addr.sa_family != AF_INET) {
89 error = EAFNOSUPPORT;
90 goto bad;
91 }
92 src = (struct sockaddr_in *)&ifp->if_addr;
93 }
94 ip->ip_src = src->sin_addr;
94a62155 95 ip->ip_ttl = MAXTTL;
ee787340 96 return (ip_output(m, (struct mbuf *)0, 0, 1));
126472ab
SL
97bad:
98 m_freem(m);
99 return (error);
2a598b25 100}