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