xref: /csrg-svn/sys/netinet/ip_output.c (revision 6339)
1 /*	ip_output.c	1.27	82/03/28	*/
2 
3 #include "../h/param.h"
4 #include "../h/mbuf.h"
5 #include "../h/mtpr.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../net/in.h"
9 #include "../net/in_systm.h"
10 #include "../net/if.h"
11 #include "../net/ip.h"
12 #include "../net/ip_var.h"
13 #include "../net/route.h"
14 
15 ip_output(m, opt, ro, allowbroadcast)
16 	struct mbuf *m;
17 	struct mbuf *opt;
18 	struct route *ro;
19 	int allowbroadcast;
20 {
21 	register struct ip *ip = mtod(m, struct ip *);
22 	register struct ifnet *ifp;
23 	int len, hlen = sizeof (struct ip), off;
24 	struct sockaddr_in tempaddr;	/* temp kludge */
25 	struct route iproute;
26 
27 COUNT(IP_OUTPUT);
28 	if (opt)				/* XXX */
29 		(void) m_free(opt);		/* XXX */
30 	/*
31 	 * Fill in IP header.
32 	 */
33 	ip->ip_v = IPVERSION;
34 	ip->ip_hl = hlen >> 2;
35 	ip->ip_off &= IP_DF;
36 	ip->ip_id = htons(ip_id++);
37 
38 #ifdef notdef
39 	/*
40 	 * Find interface for this packet.
41 	 */
42 	if (ro == 0) {
43 		ro = &iproute;
44 		bzero((caddr_t)ro, sizeof (*ro));
45 	}
46 	if (ro->ro_rt == 0) {
47 		ro->ro_dest.sin_addr = ip->ip_dst;
48 		ro->ro_dest.sin_family = AF_INET;
49 		route(ro);
50 	}
51 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0)
52 		goto bad;
53 #else
54 	/* interim kludge before routing fallout */
55 	ifp = if_ifonnetof(ip->ip_dst.s_net);
56 	if (ifp == 0)
57 		goto bad;
58 	tempaddr.sin_family = AF_INET;
59 	tempaddr.sin_addr = ip->ip_dst;
60 #endif
61 
62 	if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) {
63 		struct sockaddr_in *sin;
64 
65 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
66 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
67 			goto bad;
68 	}
69 
70 	/*
71 	 * If small enough for interface, can just send directly.
72 	 */
73 	if (ip->ip_len <= ifp->if_mtu) {
74 #if vax
75 		ip->ip_len = htons((u_short)ip->ip_len);
76 		ip->ip_off = htons((u_short)ip->ip_off);
77 #endif
78 		ip->ip_sum = 0;
79 		ip->ip_sum = in_cksum(m, hlen);
80 		return ((*ifp->if_output)(ifp, m,
81 #ifdef notdef
82 		  &ro->ro_rt->rt_dest));
83 #else
84 		  (struct sockaddr *)&tempaddr));
85 #endif
86 	}
87 
88 	/*
89 	 * Too large for interface; fragment if possible.
90 	 * Must be able to put at least 8 bytes per fragment.
91 	 */
92 	if (ip->ip_off & IP_DF)
93 		goto bad;
94 	len = (ifp->if_mtu - hlen) &~ 7;
95 	if (len < 8)
96 		goto bad;
97 
98 	/*
99 	 * Discard IP header from logical mbuf for m_copy's sake.
100 	 * Loop through length of segment, make a copy of each
101 	 * part and output.
102 	 */
103 	m->m_len -= sizeof (struct ip);
104 	m->m_off += sizeof (struct ip);
105 	for (off = 0; off < ip->ip_len-hlen; off += len) {
106 		struct mbuf *mh = m_get(M_DONTWAIT);
107 		struct ip *mhip;
108 
109 		if (mh == 0)
110 			goto bad;
111 		mh->m_off = MMAXOFF - hlen;
112 		mhip = mtod(mh, struct ip *);
113 		*mhip = *ip;
114 		if (hlen > sizeof (struct ip)) {
115 			int olen = ip_optcopy(ip, mhip, off);
116 			mh->m_len = sizeof (struct ip) + olen;
117 		} else
118 			mh->m_len = sizeof (struct ip);
119 		mhip->ip_off = off >> 3;
120 		if (off + len >= ip->ip_len-hlen)
121 			len = mhip->ip_len = ip->ip_len - hlen - off;
122 		else {
123 			mhip->ip_len = len;
124 			mhip->ip_off |= IP_MF;
125 		}
126 		mhip->ip_len += sizeof (struct ip);
127 #if vax
128 		mhip->ip_len = htons((u_short)mhip->ip_len);
129 #endif
130 		mh->m_next = m_copy(m, off, len);
131 		if (mh->m_next == 0) {
132 			(void) m_free(mh);
133 			goto bad;
134 		}
135 #if vax
136 		mhip->ip_off = htons((u_short)mhip->ip_off);
137 #endif
138 		mhip->ip_sum = 0;
139 		mhip->ip_sum = in_cksum(mh, hlen);
140 		if ((*ifp->if_output)(ifp, mh,
141 #ifdef notdef
142 		  &ro->ro_rt->rt_dest) == 0)
143 #else
144 		  (struct sockaddr *)&tempaddr) == 0)
145 #endif
146 			goto bad;
147 	}
148 	m_freem(m);
149 	return (1);
150 bad:
151 	m_freem(m);
152 	return (0);
153 }
154 
155 /*
156  * Copy options from ip to jp.
157  * If off is 0 all options are copied
158  * otherwise copy selectively.
159  */
160 ip_optcopy(ip, jp, off)
161 	struct ip *ip, *jp;
162 	int off;
163 {
164 	register u_char *cp, *dp;
165 	int opt, optlen, cnt;
166 
167 COUNT(IP_OPTCOPY);
168 	cp = (u_char *)(ip + 1);
169 	dp = (u_char *)(jp + 1);
170 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
171 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
172 		opt = cp[0];
173 		if (opt == IPOPT_EOL)
174 			break;
175 		if (opt == IPOPT_NOP)
176 			optlen = 1;
177 		else
178 			optlen = cp[1];
179 		if (optlen > cnt)			/* XXX */
180 			optlen = cnt;			/* XXX */
181 		if (off == 0 || IPOPT_COPIED(opt)) {
182 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
183 			dp += optlen;
184 		}
185 	}
186 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
187 		*dp++ = IPOPT_EOL;
188 	return (optlen);
189 }
190