+ mhip->ip_off = htons((u_short)mhip->ip_off);
+ mhip->ip_sum = 0;
+ mhip->ip_sum = in_cksum(mh, hlen);
+ if (error = (*ifp->if_output)(ifp, mh, (struct sockaddr *)dst))
+ break;
+ }
+bad:
+ m_freem(m);
+done:
+ if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
+ RTFREE(ro->ro_rt);
+ return (error);
+}
+
+/*
+ * Insert IP options into preformed packet.
+ * Adjust IP destination as required for IP source routing,
+ * as indicated by a non-zero in_addr at the start of the options.
+ */
+struct mbuf *
+ip_insertoptions(m, opt, phlen)
+ register struct mbuf *m;
+ struct mbuf *opt;
+ int *phlen;
+{
+ register struct ipoption *p = mtod(opt, struct ipoption *);
+ struct mbuf *n;
+ register struct ip *ip = mtod(m, struct ip *);
+ unsigned optlen;
+
+ optlen = opt->m_len - sizeof(p->ipopt_dst);
+ if (p->ipopt_dst.s_addr)
+ ip->ip_dst = p->ipopt_dst;
+ if (m->m_off >= MMAXOFF || MMINOFF + optlen > m->m_off) {
+ MGET(n, M_DONTWAIT, MT_HEADER);
+ if (n == 0)
+ return (m);
+ m->m_len -= sizeof(struct ip);
+ m->m_off += sizeof(struct ip);
+ n->m_next = m;
+ m = n;
+ m->m_off = MMAXOFF - sizeof(struct ip) - optlen;
+ m->m_len = optlen + sizeof(struct ip);
+ bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
+ } else {
+ m->m_off -= optlen;
+ m->m_len += optlen;
+ ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
+ }
+ ip = mtod(m, struct ip *);
+ bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
+ *phlen = sizeof(struct ip) + optlen;
+ ip->ip_len += optlen;
+ return (m);
+}
+
+/*
+ * Copy options from ip to jp.
+ * If off is 0 all options are copied
+ * otherwise copy selectively.
+ */
+ip_optcopy(ip, jp, off)
+ struct ip *ip, *jp;
+ int off;
+{
+ register u_char *cp, *dp;
+ int opt, optlen, cnt;
+
+ cp = (u_char *)(ip + 1);
+ dp = (u_char *)(jp + 1);
+ cnt = (ip->ip_hl << 2) - sizeof (struct ip);
+ for (; cnt > 0; cnt -= optlen, cp += optlen) {
+ opt = cp[0];
+ if (opt == IPOPT_EOL)
+ break;
+ if (opt == IPOPT_NOP)
+ optlen = 1;
+ else
+ optlen = cp[IPOPT_OLEN];
+ if (optlen > cnt) /* XXX */
+ optlen = cnt; /* XXX */
+ if (off == 0 || IPOPT_COPIED(opt)) {
+ bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
+ dp += optlen;