xref: /csrg-svn/sys/netinet/ip_output.c (revision 6368)
1 /*	ip_output.c	1.29	82/03/30	*/
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 route iproute;
25 	struct sockaddr *dst;
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 	/*
39 	 * Find interface for this packet in the routing
40 	 * table.  Note each interface has placed itself
41 	 * in there at boot time, so call on route degenerates
42 	 * to if_ifonnetof(ip->ip_dst.s_net).
43 	 */
44 	if (ro == 0) {
45 		ro = &iproute;
46 		bzero((caddr_t)ro, sizeof (*ro));
47 	}
48 	if (ro->ro_rt == 0) {
49 		ro->ro_dst.sa_family = AF_INET;
50 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = ip->ip_dst;
51 		rtalloc(ro);
52 	}
53 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
54 printf("no route to %x\n", ip->ip_dst.s_addr);
55 		goto bad;
56 }
57 	dst = ro->ro_rt->rt_flags&RTF_DIRECT ?
58 	    (struct sockaddr *)&ro->ro_dst : &ro->ro_rt->rt_gateway;
59 	if (!allowbroadcast && (ifp->if_flags & IFF_BROADCAST)) {
60 		struct sockaddr_in *sin;
61 
62 		sin = (struct sockaddr_in *)&ifp->if_broadaddr;
63 		if (sin->sin_addr.s_addr == ip->ip_dst.s_addr)
64 			goto bad;
65 	}
66 
67 	/*
68 	 * If small enough for interface, can just send directly.
69 	 */
70 	if (ip->ip_len <= ifp->if_mtu) {
71 #if vax
72 		ip->ip_len = htons((u_short)ip->ip_len);
73 		ip->ip_off = htons((u_short)ip->ip_off);
74 #endif
75 		ip->ip_sum = 0;
76 		ip->ip_sum = in_cksum(m, hlen);
77 		return ((*ifp->if_output)(ifp, m, dst));
78 	}
79 
80 	/*
81 	 * Too large for interface; fragment if possible.
82 	 * Must be able to put at least 8 bytes per fragment.
83 	 */
84 	if (ip->ip_off & IP_DF)
85 		goto bad;
86 	len = (ifp->if_mtu - hlen) &~ 7;
87 	if (len < 8)
88 		goto bad;
89 
90 	/*
91 	 * Discard IP header from logical mbuf for m_copy's sake.
92 	 * Loop through length of segment, make a copy of each
93 	 * part and output.
94 	 */
95 	m->m_len -= sizeof (struct ip);
96 	m->m_off += sizeof (struct ip);
97 	for (off = 0; off < ip->ip_len-hlen; off += len) {
98 		struct mbuf *mh = m_get(M_DONTWAIT);
99 		struct ip *mhip;
100 
101 		if (mh == 0)
102 			goto bad;
103 		mh->m_off = MMAXOFF - hlen;
104 		mhip = mtod(mh, struct ip *);
105 		*mhip = *ip;
106 		if (hlen > sizeof (struct ip)) {
107 			int olen = ip_optcopy(ip, mhip, off);
108 			mh->m_len = sizeof (struct ip) + olen;
109 		} else
110 			mh->m_len = sizeof (struct ip);
111 		mhip->ip_off = off >> 3;
112 		if (off + len >= ip->ip_len-hlen)
113 			len = mhip->ip_len = ip->ip_len - hlen - off;
114 		else {
115 			mhip->ip_len = len;
116 			mhip->ip_off |= IP_MF;
117 		}
118 		mhip->ip_len += sizeof (struct ip);
119 #if vax
120 		mhip->ip_len = htons((u_short)mhip->ip_len);
121 #endif
122 		mh->m_next = m_copy(m, off, len);
123 		if (mh->m_next == 0) {
124 			(void) m_free(mh);
125 			goto bad;
126 		}
127 #if vax
128 		mhip->ip_off = htons((u_short)mhip->ip_off);
129 #endif
130 		mhip->ip_sum = 0;
131 		mhip->ip_sum = in_cksum(mh, hlen);
132 		if ((*ifp->if_output)(ifp, mh, dst) == 0)
133 			goto bad;
134 	}
135 	m_freem(m);
136 	return (1);
137 bad:
138 	m_freem(m);
139 	return (0);
140 }
141 
142 /*
143  * Copy options from ip to jp.
144  * If off is 0 all options are copied
145  * otherwise copy selectively.
146  */
147 ip_optcopy(ip, jp, off)
148 	struct ip *ip, *jp;
149 	int off;
150 {
151 	register u_char *cp, *dp;
152 	int opt, optlen, cnt;
153 
154 COUNT(IP_OPTCOPY);
155 	cp = (u_char *)(ip + 1);
156 	dp = (u_char *)(jp + 1);
157 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
158 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
159 		opt = cp[0];
160 		if (opt == IPOPT_EOL)
161 			break;
162 		if (opt == IPOPT_NOP)
163 			optlen = 1;
164 		else
165 			optlen = cp[1];
166 		if (optlen > cnt)			/* XXX */
167 			optlen = cnt;			/* XXX */
168 		if (off == 0 || IPOPT_COPIED(opt)) {
169 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
170 			dp += optlen;
171 		}
172 	}
173 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
174 		*dp++ = IPOPT_EOL;
175 	return (optlen);
176 }
177