Commit | Line | Data |
---|---|---|
f4d55810 | 1 | /* ip_output.c 1.46 83/02/10 */ |
6e8b2eca | 2 | |
ac904f92 | 3 | #include "../h/param.h" |
dad64fdf | 4 | #include "../h/mbuf.h" |
f4d55810 | 5 | #include "../h/errno.h" |
dad64fdf | 6 | #include "../h/socket.h" |
2b4b57cd | 7 | #include "../h/socketvar.h" |
f4d55810 SL |
8 | |
9 | #include "../net/if.h" | |
10 | #include "../net/route.h" | |
11 | ||
fcfe450e BJ |
12 | #include "../netinet/in.h" |
13 | #include "../netinet/in_systm.h" | |
fcfe450e BJ |
14 | #include "../netinet/ip.h" |
15 | #include "../netinet/ip_var.h" | |
f4d55810 SL |
16 | |
17 | #include "../machine/mtpr.h" | |
ac904f92 | 18 | |
a13c006d BJ |
19 | int ipnorouteprint = 0; |
20 | ||
ee787340 | 21 | ip_output(m, opt, ro, allowbroadcast) |
2b4b57cd | 22 | struct mbuf *m; |
8a13b737 | 23 | struct mbuf *opt; |
ee787340 | 24 | struct route *ro; |
1e977657 | 25 | int allowbroadcast; |
ac904f92 | 26 | { |
2b4b57cd | 27 | register struct ip *ip = mtod(m, struct ip *); |
8a13b737 | 28 | register struct ifnet *ifp; |
8a2f82db | 29 | int len, hlen = sizeof (struct ip), off, error = 0; |
ee787340 | 30 | struct route iproute; |
c124e997 | 31 | struct sockaddr *dst; |
ac904f92 | 32 | |
2d82c10d | 33 | if (opt) /* XXX */ |
89e6227e | 34 | (void) m_free(opt); /* XXX */ |
e8d11875 | 35 | /* |
2b4b57cd | 36 | * Fill in IP header. |
e8d11875 | 37 | */ |
2b4b57cd BJ |
38 | ip->ip_v = IPVERSION; |
39 | ip->ip_hl = hlen >> 2; | |
40 | ip->ip_off &= IP_DF; | |
8a13b737 BJ |
41 | ip->ip_id = htons(ip_id++); |
42 | ||
43 | /* | |
a13c006d | 44 | * Route packet. |
8a13b737 | 45 | */ |
ee787340 SL |
46 | if (ro == 0) { |
47 | ro = &iproute; | |
48 | bzero((caddr_t)ro, sizeof (*ro)); | |
49 | } | |
a13c006d | 50 | dst = &ro->ro_dst; |
ee787340 | 51 | if (ro->ro_rt == 0) { |
f6311fb6 SL |
52 | ro->ro_dst.sa_family = AF_INET; |
53 | ((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst; | |
a13c006d BJ |
54 | /* |
55 | * If routing to interface only, short circuit routing lookup. | |
56 | */ | |
57 | if (ro == &routetoif) { | |
58 | /* check ifp is AF_INET??? */ | |
a1edc12b | 59 | ifp = if_ifonnetof(in_netof(ip->ip_dst)); |
a13c006d BJ |
60 | if (ifp == 0) |
61 | goto unreachable; | |
62 | goto gotif; | |
63 | } | |
f6311fb6 | 64 | rtalloc(ro); |
8a13b737 | 65 | } |
a13c006d BJ |
66 | if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) |
67 | goto unreachable; | |
68 | ro->ro_rt->rt_use++; | |
69 | if (ro->ro_rt->rt_flags & RTF_GATEWAY) | |
70 | dst = &ro->ro_rt->rt_gateway; | |
71 | gotif: | |
af099287 | 72 | #ifndef notdef |
a13c006d BJ |
73 | /* |
74 | * If source address not specified yet, use address | |
75 | * of outgoing interface. | |
76 | */ | |
af099287 | 77 | if (in_lnaof(ip->ip_src) == INADDR_ANY) |
a13c006d BJ |
78 | ip->ip_src.s_addr = |
79 | ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr; | |
af099287 | 80 | #endif |
8a2f82db | 81 | |
a13c006d | 82 | /* |
af099287 SL |
83 | * Look for broadcast address and |
84 | * and verify user is allowed to send | |
19f96414 | 85 | * such a packet. |
a13c006d | 86 | */ |
19f96414 | 87 | if (in_lnaof(dst) == INADDR_ANY) { |
19f96414 SL |
88 | if ((ifp->if_flags & IFF_BROADCAST) == 0) { |
89 | error = EADDRNOTAVAIL; | |
90 | goto bad; | |
91 | } | |
92 | if (!allowbroadcast) { | |
a13c006d | 93 | error = EACCES; |
ee787340 | 94 | goto bad; |
8a2f82db | 95 | } |
19f96414 SL |
96 | /* don't allow broadcast messages to be fragmented */ |
97 | if (ip->ip_len > ifp->if_mtu) { | |
98 | error = EMSGSIZE; | |
99 | goto bad; | |
100 | } | |
ee787340 | 101 | } |
ac904f92 | 102 | |
2b4b57cd BJ |
103 | /* |
104 | * If small enough for interface, can just send directly. | |
105 | */ | |
8a13b737 BJ |
106 | if (ip->ip_len <= ifp->if_mtu) { |
107 | ip->ip_len = htons((u_short)ip->ip_len); | |
108 | ip->ip_off = htons((u_short)ip->ip_off); | |
109 | ip->ip_sum = 0; | |
110 | ip->ip_sum = in_cksum(m, hlen); | |
a13c006d BJ |
111 | error = (*ifp->if_output)(ifp, m, dst); |
112 | goto done; | |
cdad2eb1 | 113 | } |
2b4b57cd BJ |
114 | |
115 | /* | |
116 | * Too large for interface; fragment if possible. | |
117 | * Must be able to put at least 8 bytes per fragment. | |
118 | */ | |
8a2f82db SL |
119 | if (ip->ip_off & IP_DF) { |
120 | error = EMSGSIZE; | |
2b4b57cd | 121 | goto bad; |
8a2f82db | 122 | } |
8a13b737 | 123 | len = (ifp->if_mtu - hlen) &~ 7; |
8a2f82db SL |
124 | if (len < 8) { |
125 | error = EMSGSIZE; | |
2b4b57cd | 126 | goto bad; |
8a2f82db | 127 | } |
2b4b57cd BJ |
128 | |
129 | /* | |
130 | * Discard IP header from logical mbuf for m_copy's sake. | |
131 | * Loop through length of segment, make a copy of each | |
132 | * part and output. | |
133 | */ | |
134 | m->m_len -= sizeof (struct ip); | |
135 | m->m_off += sizeof (struct ip); | |
0b49870f | 136 | for (off = 0; off < ip->ip_len-hlen; off += len) { |
22bd8cdc | 137 | struct mbuf *mh = m_get(M_DONTWAIT, MT_HEADER); |
2b4b57cd BJ |
138 | struct ip *mhip; |
139 | ||
8a2f82db SL |
140 | if (mh == 0) { |
141 | error = ENOBUFS; | |
2b4b57cd | 142 | goto bad; |
8a2f82db | 143 | } |
2b4b57cd BJ |
144 | mh->m_off = MMAXOFF - hlen; |
145 | mhip = mtod(mh, struct ip *); | |
146 | *mhip = *ip; | |
4ad99bae | 147 | if (hlen > sizeof (struct ip)) { |
2b4b57cd BJ |
148 | int olen = ip_optcopy(ip, mhip, off); |
149 | mh->m_len = sizeof (struct ip) + olen; | |
150 | } else | |
151 | mh->m_len = sizeof (struct ip); | |
e8f40dc9 | 152 | mhip->ip_off = off >> 3; |
0b49870f BJ |
153 | if (off + len >= ip->ip_len-hlen) |
154 | len = mhip->ip_len = ip->ip_len - hlen - off; | |
2b4b57cd BJ |
155 | else { |
156 | mhip->ip_len = len; | |
157 | mhip->ip_off |= IP_MF; | |
98444525 | 158 | } |
e8f40dc9 | 159 | mhip->ip_len += sizeof (struct ip); |
e8f40dc9 | 160 | mhip->ip_len = htons((u_short)mhip->ip_len); |
2b4b57cd BJ |
161 | mh->m_next = m_copy(m, off, len); |
162 | if (mh->m_next == 0) { | |
2752c877 | 163 | (void) m_free(mh); |
8a2f82db | 164 | error = ENOBUFS; /* ??? */ |
2b4b57cd | 165 | goto bad; |
98444525 | 166 | } |
0b49870f | 167 | mhip->ip_off = htons((u_short)mhip->ip_off); |
0b49870f BJ |
168 | mhip->ip_sum = 0; |
169 | mhip->ip_sum = in_cksum(mh, hlen); | |
8a2f82db SL |
170 | if (error = (*ifp->if_output)(ifp, mh, dst)) |
171 | break; | |
2b4b57cd | 172 | } |
a13c006d BJ |
173 | m_freem(m); |
174 | goto done; | |
175 | ||
176 | unreachable: | |
177 | if (ipnorouteprint) | |
178 | printf("no route to %x (from %x, len %d)\n", | |
179 | ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len); | |
180 | error = ENETUNREACH; | |
2b4b57cd BJ |
181 | bad: |
182 | m_freem(m); | |
a13c006d BJ |
183 | done: |
184 | if (ro == &iproute && ro->ro_rt) | |
185 | RTFREE(ro->ro_rt); | |
8a2f82db | 186 | return (error); |
2b4b57cd BJ |
187 | } |
188 | ||
189 | /* | |
190 | * Copy options from ip to jp. | |
4ad99bae | 191 | * If off is 0 all options are copied |
2b4b57cd BJ |
192 | * otherwise copy selectively. |
193 | */ | |
194 | ip_optcopy(ip, jp, off) | |
195 | struct ip *ip, *jp; | |
196 | int off; | |
197 | { | |
198 | register u_char *cp, *dp; | |
199 | int opt, optlen, cnt; | |
200 | ||
201 | cp = (u_char *)(ip + 1); | |
202 | dp = (u_char *)(jp + 1); | |
203 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); | |
204 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
205 | opt = cp[0]; | |
206 | if (opt == IPOPT_EOL) | |
207 | break; | |
208 | if (opt == IPOPT_NOP) | |
209 | optlen = 1; | |
210 | else | |
211 | optlen = cp[1]; | |
212 | if (optlen > cnt) /* XXX */ | |
213 | optlen = cnt; /* XXX */ | |
214 | if (off == 0 || IPOPT_COPIED(opt)) { | |
4ad99bae | 215 | bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); |
2b4b57cd | 216 | dp += optlen; |
ac904f92 | 217 | } |
e8d11875 | 218 | } |
2b4b57cd BJ |
219 | for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) |
220 | *dp++ = IPOPT_EOL; | |
221 | return (optlen); | |
ac904f92 | 222 | } |