xref: /original-bsd/sys/netinet/ip_output.c (revision e5dafb11)
1 /*	ip_output.c	1.26	82/03/15	*/
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 
14 ip_output(m, opt, allowbroadcast)
15 	struct mbuf *m;
16 	struct mbuf *opt;
17 	int allowbroadcast;
18 {
19 	register struct ip *ip = mtod(m, struct ip *);
20 	register struct ifnet *ifp;
21 	int len, hlen = sizeof (struct ip), off;
22 
23 COUNT(IP_OUTPUT);
24 	if (opt)				/* XXX */
25 		(void) m_free(opt);		/* XXX */
26 	/*
27 	 * Fill in IP header.
28 	 */
29 	ip->ip_v = IPVERSION;
30 	ip->ip_hl = hlen >> 2;
31 	ip->ip_off &= IP_DF;
32 	ip->ip_id = htons(ip_id++);
33 
34 	/*
35 	 * Find interface for this packet.
36 	 */
37 	ifp = if_ifonnetof(ip->ip_dst);
38 	if (ifp == 0) {
39 		ifp = if_gatewayfor(ip->ip_dst);
40 		if (ifp == 0)
41 			goto bad;
42 	}
43 	if (!allowbroadcast && ifp->if_broadaddr.s_addr != 0 &&
44 	    ifp->if_broadaddr.s_addr == ip->ip_dst.s_addr)
45 		goto bad;
46 
47 	/*
48 	 * If small enough for interface, can just send directly.
49 	 */
50 	if (ip->ip_len <= ifp->if_mtu) {
51 #if vax
52 		ip->ip_len = htons((u_short)ip->ip_len);
53 		ip->ip_off = htons((u_short)ip->ip_off);
54 #endif
55 		ip->ip_sum = 0;
56 		ip->ip_sum = in_cksum(m, hlen);
57 		return ((*ifp->if_output)(ifp, m, PF_INET));
58 	}
59 
60 	/*
61 	 * Too large for interface; fragment if possible.
62 	 * Must be able to put at least 8 bytes per fragment.
63 	 */
64 	if (ip->ip_off & IP_DF)
65 		goto bad;
66 	len = (ifp->if_mtu - hlen) &~ 7;
67 	if (len < 8)
68 		goto bad;
69 
70 	/*
71 	 * Discard IP header from logical mbuf for m_copy's sake.
72 	 * Loop through length of segment, make a copy of each
73 	 * part and output.
74 	 */
75 	m->m_len -= sizeof (struct ip);
76 	m->m_off += sizeof (struct ip);
77 	for (off = 0; off < ip->ip_len-hlen; off += len) {
78 		struct mbuf *mh = m_get(M_DONTWAIT);
79 		struct ip *mhip;
80 
81 		if (mh == 0)
82 			goto bad;
83 		mh->m_off = MMAXOFF - hlen;
84 		mhip = mtod(mh, struct ip *);
85 		*mhip = *ip;
86 		if (hlen > sizeof (struct ip)) {
87 			int olen = ip_optcopy(ip, mhip, off);
88 			mh->m_len = sizeof (struct ip) + olen;
89 		} else
90 			mh->m_len = sizeof (struct ip);
91 		mhip->ip_off = off >> 3;
92 		if (off + len >= ip->ip_len-hlen)
93 			len = mhip->ip_len = ip->ip_len - hlen - off;
94 		else {
95 			mhip->ip_len = len;
96 			mhip->ip_off |= IP_MF;
97 		}
98 		mhip->ip_len += sizeof (struct ip);
99 #if vax
100 		mhip->ip_len = htons((u_short)mhip->ip_len);
101 #endif
102 		mh->m_next = m_copy(m, off, len);
103 		if (mh->m_next == 0) {
104 			(void) m_free(mh);
105 			goto bad;
106 		}
107 #if vax
108 		mhip->ip_off = htons((u_short)mhip->ip_off);
109 #endif
110 		mhip->ip_sum = 0;
111 		mhip->ip_sum = in_cksum(mh, hlen);
112 		if ((*ifp->if_output)(ifp, mh, PF_INET) == 0)
113 			goto bad;
114 	}
115 	m_freem(m);
116 	return (1);
117 bad:
118 	m_freem(m);
119 	return (0);
120 }
121 
122 /*
123  * Copy options from ip to jp.
124  * If off is 0 all options are copied
125  * otherwise copy selectively.
126  */
127 ip_optcopy(ip, jp, off)
128 	struct ip *ip, *jp;
129 	int off;
130 {
131 	register u_char *cp, *dp;
132 	int opt, optlen, cnt;
133 
134 COUNT(IP_OPTCOPY);
135 	cp = (u_char *)(ip + 1);
136 	dp = (u_char *)(jp + 1);
137 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
138 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
139 		opt = cp[0];
140 		if (opt == IPOPT_EOL)
141 			break;
142 		if (opt == IPOPT_NOP)
143 			optlen = 1;
144 		else
145 			optlen = cp[1];
146 		if (optlen > cnt)			/* XXX */
147 			optlen = cnt;			/* XXX */
148 		if (off == 0 || IPOPT_COPIED(opt)) {
149 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
150 			dp += optlen;
151 		}
152 	}
153 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
154 		*dp++ = IPOPT_EOL;
155 	return (optlen);
156 }
157