don't allow open O_RDWR if no write ring
[unix-history] / usr / src / sys / netinet / ip_output.c
CommitLineData
5afc289d
MK
1/*
2 * Copyright (c) 1982 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
6 * @(#)ip_output.c 6.8 (Berkeley) %G%
7 */
6e8b2eca 8
20666ad3
JB
9#include "param.h"
10#include "mbuf.h"
11#include "errno.h"
12#include "socket.h"
13#include "socketvar.h"
f4d55810
SL
14
15#include "../net/if.h"
16#include "../net/route.h"
17
20666ad3
JB
18#include "in.h"
19#include "in_systm.h"
dfce8240 20#include "in_var.h"
20666ad3
JB
21#include "ip.h"
22#include "ip_var.h"
f4d55810 23
b152d3c4
SL
24#ifdef vax
25#include "../vax/mtpr.h"
26#endif
ac904f92 27
0e3f761f 28ip_output(m, opt, ro, flags)
2b4b57cd 29 struct mbuf *m;
8a13b737 30 struct mbuf *opt;
ee787340 31 struct route *ro;
0e3f761f 32 int flags;
ac904f92 33{
2b4b57cd 34 register struct ip *ip = mtod(m, struct ip *);
8a13b737 35 register struct ifnet *ifp;
8a2f82db 36 int len, hlen = sizeof (struct ip), off, error = 0;
ee787340 37 struct route iproute;
a8671e7e 38 struct sockaddr_in *dst;
ac904f92 39
2d82c10d 40 if (opt) /* XXX */
89e6227e 41 (void) m_free(opt); /* XXX */
e8d11875 42 /*
2b4b57cd 43 * Fill in IP header.
e8d11875 44 */
0e3f761f
SL
45 if ((flags & IP_FORWARDING) == 0) {
46 ip->ip_v = IPVERSION;
47 ip->ip_off &= IP_DF;
48 ip->ip_id = htons(ip_id++);
4adcd589 49 ip->ip_hl = hlen >> 2;
dfce8240 50 }
8a13b737
BJ
51
52 /*
a13c006d 53 * Route packet.
8a13b737 54 */
ee787340
SL
55 if (ro == 0) {
56 ro = &iproute;
57 bzero((caddr_t)ro, sizeof (*ro));
58 }
a8671e7e 59 dst = (struct sockaddr_in *)&ro->ro_dst;
ee787340 60 if (ro->ro_rt == 0) {
a8671e7e
SL
61 dst->sin_family = AF_INET;
62 dst->sin_addr = ip->ip_dst;
a13c006d 63 /*
0e3f761f
SL
64 * If routing to interface only,
65 * short circuit routing lookup.
a13c006d 66 */
0e3f761f 67 if (flags & IP_ROUTETOIF) {
dfce8240
MK
68 struct in_ifaddr *ia;
69 ia = in_iaonnetof(in_netof(ip->ip_dst));
70 if (ia == 0) {
0e3f761f
SL
71 error = ENETUNREACH;
72 goto bad;
73 }
dfce8240 74 ifp = ia->ia_ifp;
a13c006d
BJ
75 goto gotif;
76 }
f6311fb6 77 rtalloc(ro);
4adcd589 78 } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
ffb7b765
MK
79 /*
80 * The old route has gone away; try for a new one.
81 */
82 rtfree(ro->ro_rt);
dfce8240 83 ro->ro_rt = NULL;
ffb7b765 84 rtalloc(ro);
8a13b737 85 }
0e3f761f
SL
86 if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
87 error = ENETUNREACH;
88 goto bad;
89 }
a13c006d 90 ro->ro_rt->rt_use++;
ebef8376 91 if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
a8671e7e 92 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
a13c006d 93gotif:
5afc289d
MK
94#ifndef notdef
95 /*
96 * If source address not specified yet, use address
97 * of outgoing interface.
98 */
99 if (ip->ip_src.s_addr == INADDR_ANY) {
100 register struct in_ifaddr *ia;
101
102 for (ia = in_ifaddr; ia; ia = ia->ia_next)
103 if (ia->ia_ifp == ifp) {
104 ip->ip_src = IA_SIN(ia)->sin_addr;
105 break;
106 }
107 }
108#endif
a13c006d 109 /*
af099287
SL
110 * Look for broadcast address and
111 * and verify user is allowed to send
19f96414 112 * such a packet.
a13c006d 113 */
dfce8240 114 if (in_broadcast(dst->sin_addr)) {
19f96414
SL
115 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
116 error = EADDRNOTAVAIL;
117 goto bad;
118 }
0e3f761f 119 if ((flags & IP_ALLOWBROADCAST) == 0) {
a13c006d 120 error = EACCES;
ee787340 121 goto bad;
8a2f82db 122 }
19f96414
SL
123 /* don't allow broadcast messages to be fragmented */
124 if (ip->ip_len > ifp->if_mtu) {
125 error = EMSGSIZE;
126 goto bad;
127 }
ee787340 128 }
ac904f92 129
2b4b57cd
BJ
130 /*
131 * If small enough for interface, can just send directly.
132 */
8a13b737
BJ
133 if (ip->ip_len <= ifp->if_mtu) {
134 ip->ip_len = htons((u_short)ip->ip_len);
135 ip->ip_off = htons((u_short)ip->ip_off);
136 ip->ip_sum = 0;
137 ip->ip_sum = in_cksum(m, hlen);
a8671e7e 138 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
a13c006d 139 goto done;
cdad2eb1 140 }
2b4b57cd
BJ
141
142 /*
143 * Too large for interface; fragment if possible.
144 * Must be able to put at least 8 bytes per fragment.
145 */
8a2f82db
SL
146 if (ip->ip_off & IP_DF) {
147 error = EMSGSIZE;
2b4b57cd 148 goto bad;
8a2f82db 149 }
8a13b737 150 len = (ifp->if_mtu - hlen) &~ 7;
8a2f82db
SL
151 if (len < 8) {
152 error = EMSGSIZE;
2b4b57cd 153 goto bad;
8a2f82db 154 }
2b4b57cd
BJ
155
156 /*
157 * Discard IP header from logical mbuf for m_copy's sake.
158 * Loop through length of segment, make a copy of each
159 * part and output.
160 */
161 m->m_len -= sizeof (struct ip);
162 m->m_off += sizeof (struct ip);
0b49870f 163 for (off = 0; off < ip->ip_len-hlen; off += len) {
22bd8cdc 164 struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER);
2b4b57cd
BJ
165 struct ip *mhip;
166
8a2f82db
SL
167 if (mh == 0) {
168 error = ENOBUFS;
2b4b57cd 169 goto bad;
8a2f82db 170 }
2b4b57cd
BJ
171 mh->m_off = MMAXOFF - hlen;
172 mhip = mtod(mh, struct ip *);
173 *mhip = *ip;
4ad99bae 174 if (hlen > sizeof (struct ip)) {
2b4b57cd
BJ
175 int olen = ip_optcopy(ip, mhip, off);
176 mh->m_len = sizeof (struct ip) + olen;
177 } else
178 mh->m_len = sizeof (struct ip);
e8f40dc9 179 mhip->ip_off = off >> 3;
4adcd589
MK
180 if (ip->ip_off & IP_MF)
181 mhip->ip_off |= IP_MF;
0b49870f
BJ
182 if (off + len >= ip->ip_len-hlen)
183 len = mhip->ip_len = ip->ip_len - hlen - off;
2b4b57cd
BJ
184 else {
185 mhip->ip_len = len;
186 mhip->ip_off |= IP_MF;
98444525 187 }
e8f40dc9 188 mhip->ip_len += sizeof (struct ip);
e8f40dc9 189 mhip->ip_len = htons((u_short)mhip->ip_len);
2b4b57cd
BJ
190 mh->m_next = m_copy(m, off, len);
191 if (mh->m_next == 0) {
2752c877 192 (void) m_free(mh);
8a2f82db 193 error = ENOBUFS; /* ??? */
2b4b57cd 194 goto bad;
98444525 195 }
0b49870f 196 mhip->ip_off = htons((u_short)mhip->ip_off);
0b49870f
BJ
197 mhip->ip_sum = 0;
198 mhip->ip_sum = in_cksum(mh, hlen);
a8671e7e 199 if (error = (*ifp->if_output)(ifp, mh, (struct sockaddr *)dst))
8a2f82db 200 break;
2b4b57cd
BJ
201 }
202bad:
203 m_freem(m);
a13c006d 204done:
0e3f761f 205 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
a13c006d 206 RTFREE(ro->ro_rt);
8a2f82db 207 return (error);
2b4b57cd
BJ
208}
209
210/*
211 * Copy options from ip to jp.
4ad99bae 212 * If off is 0 all options are copied
2b4b57cd
BJ
213 * otherwise copy selectively.
214 */
215ip_optcopy(ip, jp, off)
216 struct ip *ip, *jp;
217 int off;
218{
219 register u_char *cp, *dp;
220 int opt, optlen, cnt;
221
222 cp = (u_char *)(ip + 1);
223 dp = (u_char *)(jp + 1);
224 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
225 for (; cnt > 0; cnt -= optlen, cp += optlen) {
226 opt = cp[0];
227 if (opt == IPOPT_EOL)
228 break;
229 if (opt == IPOPT_NOP)
230 optlen = 1;
231 else
232 optlen = cp[1];
233 if (optlen > cnt) /* XXX */
234 optlen = cnt; /* XXX */
235 if (off == 0 || IPOPT_COPIED(opt)) {
4ad99bae 236 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
2b4b57cd 237 dp += optlen;
ac904f92 238 }
e8d11875 239 }
2b4b57cd
BJ
240 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
241 *dp++ = IPOPT_EOL;
242 return (optlen);
ac904f92 243}