raw routing
[unix-history] / usr / src / sys / netinet / raw_ip.c
CommitLineData
48c8199a 1/* raw_ip.c 4.19 83/06/30 */
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"
f4d55810
SL
8#include "../h/errno.h"
9
126472ab 10#include "../net/if.h"
f4d55810 11#include "../net/route.h"
48c8199a 12#include "../net/raw_cb.h"
f4d55810 13
fcfe450e
BJ
14#include "../netinet/in.h"
15#include "../netinet/in_systm.h"
16#include "../netinet/ip.h"
17#include "../netinet/ip_var.h"
2a598b25
BJ
18
19/*
94a62155
BJ
20 * Raw interface to IP protocol.
21 */
22
48c8199a
SL
23struct sockaddr_in ripdst = { AF_INET };
24struct sockaddr_in ripsrc = { AF_INET };
25struct sockproto ripproto = { PF_INET };
94a62155
BJ
26/*
27 * Setup generic address and protocol structures
28 * for raw_input routine, then pass them along with
29 * mbuf chain.
2a598b25 30 */
2a598b25
BJ
31rip_input(m)
32 struct mbuf *m;
33{
94a62155 34 register struct ip *ip = mtod(m, struct ip *);
2a598b25 35
94a62155 36 ripproto.sp_protocol = ip->ip_p;
faad37c0
SL
37 ripdst.sin_addr = ip->ip_dst;
38 ripsrc.sin_addr = ip->ip_src;
02122dcc
SL
39 raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
40 (struct sockaddr *)&ripdst);
2a598b25
BJ
41}
42
94a62155
BJ
43/*
44 * Generate IP header and pass packet to ip_output.
45 * Tack on options user may have setup with control call.
46 */
47rip_output(m0, so)
48 struct mbuf *m0;
49 struct socket *so;
2a598b25 50{
94a62155
BJ
51 register struct mbuf *m;
52 register struct ip *ip;
126472ab
SL
53 int len = 0, error;
54 struct rawcb *rp = sotorawcb(so);
a13c006d 55 struct sockaddr_in *sin;
2a598b25 56
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;
cce93e4b 63 m = m_get(M_DONTWAIT, MT_HEADER);
94a62155 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;
a13c006d
BJ
78 if (rp->rcb_flags & RAW_LADDR) {
79 sin = (struct sockaddr_in *)&rp->rcb_laddr;
80 if (sin->sin_family != AF_INET) {
126472ab
SL
81 error = EAFNOSUPPORT;
82 goto bad;
83 }
a13c006d
BJ
84 ip->ip_src.s_addr = sin->sin_addr.s_addr;
85 } else
86 ip->ip_src.s_addr = 0;
87 ip->ip_dst = ((struct sockaddr_in *)&rp->rcb_faddr)->sin_addr;
94a62155 88 ip->ip_ttl = MAXTTL;
48c8199a 89 return (ip_output(m, (struct mbuf *)0, &rp->rcb_route,
0e3f761f 90 IP_ROUTETOIF|IP_ALLOWBROADCAST));
126472ab
SL
91bad:
92 m_freem(m);
93 return (error);
2a598b25 94}