Commit | Line | Data |
---|---|---|
4d06a026 | 1 | /* ip_output.c 1.37 82/10/13 */ |
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??? */ | |
6187f8f4 | 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: | |
69 | /* | |
70 | * If source address not specified yet, use address | |
71 | * of outgoing interface. | |
72 | */ | |
73 | if (ip->ip_src.s_addr == 0) | |
74 | ip->ip_src.s_addr = | |
75 | ((struct sockaddr_in *)&ifp->if_addr)->sin_addr.s_addr; | |
8a2f82db | 76 | |
a13c006d BJ |
77 | /* |
78 | * Have interface for packet. Allow | |
79 | * broadcasts only by authorized users. | |
80 | */ | |
ee787340 SL |
81 | if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) { |
82 | struct sockaddr_in *sin; | |
83 | ||
84 | sin = (struct sockaddr_in *)&ifp->if_broadaddr; | |
8a2f82db | 85 | if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) { |
a13c006d | 86 | error = EACCES; |
ee787340 | 87 | goto bad; |
8a2f82db | 88 | } |
ee787340 | 89 | } |
ac904f92 | 90 | |
2b4b57cd BJ |
91 | /* |
92 | * If small enough for interface, can just send directly. | |
93 | */ | |
8a13b737 | 94 | if (ip->ip_len <= ifp->if_mtu) { |
2d82c10d | 95 | #if vax |
8a13b737 BJ |
96 | ip->ip_len = htons((u_short)ip->ip_len); |
97 | ip->ip_off = htons((u_short)ip->ip_off); | |
2d82c10d | 98 | #endif |
8a13b737 BJ |
99 | ip->ip_sum = 0; |
100 | ip->ip_sum = in_cksum(m, hlen); | |
a13c006d BJ |
101 | error = (*ifp->if_output)(ifp, m, dst); |
102 | goto done; | |
cdad2eb1 | 103 | } |
2b4b57cd BJ |
104 | |
105 | /* | |
106 | * Too large for interface; fragment if possible. | |
107 | * Must be able to put at least 8 bytes per fragment. | |
108 | */ | |
8a2f82db SL |
109 | if (ip->ip_off & IP_DF) { |
110 | error = EMSGSIZE; | |
2b4b57cd | 111 | goto bad; |
8a2f82db | 112 | } |
8a13b737 | 113 | len = (ifp->if_mtu - hlen) &~ 7; |
8a2f82db SL |
114 | if (len < 8) { |
115 | error = EMSGSIZE; | |
2b4b57cd | 116 | goto bad; |
8a2f82db | 117 | } |
2b4b57cd BJ |
118 | |
119 | /* | |
120 | * Discard IP header from logical mbuf for m_copy's sake. | |
121 | * Loop through length of segment, make a copy of each | |
122 | * part and output. | |
123 | */ | |
124 | m->m_len -= sizeof (struct ip); | |
125 | m->m_off += sizeof (struct ip); | |
0b49870f | 126 | for (off = 0; off < ip->ip_len-hlen; off += len) { |
ef9b4258 | 127 | struct mbuf *mh = m_get(M_DONTWAIT); |
2b4b57cd BJ |
128 | struct ip *mhip; |
129 | ||
8a2f82db SL |
130 | if (mh == 0) { |
131 | error = ENOBUFS; | |
2b4b57cd | 132 | goto bad; |
8a2f82db | 133 | } |
2b4b57cd BJ |
134 | mh->m_off = MMAXOFF - hlen; |
135 | mhip = mtod(mh, struct ip *); | |
136 | *mhip = *ip; | |
4ad99bae | 137 | if (hlen > sizeof (struct ip)) { |
2b4b57cd BJ |
138 | int olen = ip_optcopy(ip, mhip, off); |
139 | mh->m_len = sizeof (struct ip) + olen; | |
140 | } else | |
141 | mh->m_len = sizeof (struct ip); | |
e8f40dc9 | 142 | mhip->ip_off = off >> 3; |
0b49870f BJ |
143 | if (off + len >= ip->ip_len-hlen) |
144 | len = mhip->ip_len = ip->ip_len - hlen - off; | |
2b4b57cd BJ |
145 | else { |
146 | mhip->ip_len = len; | |
147 | mhip->ip_off |= IP_MF; | |
98444525 | 148 | } |
e8f40dc9 BJ |
149 | mhip->ip_len += sizeof (struct ip); |
150 | #if vax | |
151 | mhip->ip_len = htons((u_short)mhip->ip_len); | |
152 | #endif | |
2b4b57cd BJ |
153 | mh->m_next = m_copy(m, off, len); |
154 | if (mh->m_next == 0) { | |
2752c877 | 155 | (void) m_free(mh); |
8a2f82db | 156 | error = ENOBUFS; /* ??? */ |
2b4b57cd | 157 | goto bad; |
98444525 | 158 | } |
e8f40dc9 | 159 | #if vax |
0b49870f | 160 | mhip->ip_off = htons((u_short)mhip->ip_off); |
e8f40dc9 | 161 | #endif |
0b49870f BJ |
162 | mhip->ip_sum = 0; |
163 | mhip->ip_sum = in_cksum(mh, hlen); | |
8a2f82db SL |
164 | if (error = (*ifp->if_output)(ifp, mh, dst)) |
165 | break; | |
2b4b57cd | 166 | } |
a13c006d BJ |
167 | m_freem(m); |
168 | goto done; | |
169 | ||
170 | unreachable: | |
171 | if (ipnorouteprint) | |
172 | printf("no route to %x (from %x, len %d)\n", | |
173 | ip->ip_dst.s_addr, ip->ip_src.s_addr, ip->ip_len); | |
174 | error = ENETUNREACH; | |
2b4b57cd BJ |
175 | bad: |
176 | m_freem(m); | |
a13c006d BJ |
177 | done: |
178 | if (ro == &iproute && ro->ro_rt) | |
179 | RTFREE(ro->ro_rt); | |
8a2f82db | 180 | return (error); |
2b4b57cd BJ |
181 | } |
182 | ||
183 | /* | |
184 | * Copy options from ip to jp. | |
4ad99bae | 185 | * If off is 0 all options are copied |
2b4b57cd BJ |
186 | * otherwise copy selectively. |
187 | */ | |
188 | ip_optcopy(ip, jp, off) | |
189 | struct ip *ip, *jp; | |
190 | int off; | |
191 | { | |
192 | register u_char *cp, *dp; | |
193 | int opt, optlen, cnt; | |
194 | ||
195 | cp = (u_char *)(ip + 1); | |
196 | dp = (u_char *)(jp + 1); | |
197 | cnt = (ip->ip_hl << 2) - sizeof (struct ip); | |
198 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
199 | opt = cp[0]; | |
200 | if (opt == IPOPT_EOL) | |
201 | break; | |
202 | if (opt == IPOPT_NOP) | |
203 | optlen = 1; | |
204 | else | |
205 | optlen = cp[1]; | |
206 | if (optlen > cnt) /* XXX */ | |
207 | optlen = cnt; /* XXX */ | |
208 | if (off == 0 || IPOPT_COPIED(opt)) { | |
4ad99bae | 209 | bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); |
2b4b57cd | 210 | dp += optlen; |
ac904f92 | 211 | } |
e8d11875 | 212 | } |
2b4b57cd BJ |
213 | for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) |
214 | *dp++ = IPOPT_EOL; | |
215 | return (optlen); | |
ac904f92 | 216 | } |