+
+ /* In case of IPv4-mapped address used in previous send */
+ if (ROUTE_UNUSABLE(&in6p->in6p_route) ||
+ rt_key(in6p->in6p_route.ro_rt)->sa_family != AF_INET6)
+ ROUTE_RELEASE(&in6p->in6p_route);
+
+ /* Copy the cached route and take an extra reference */
+ in6p_route_copyout(in6p, &ro);
+
+ set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
+
+ m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
+ m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
+ m->m_pkthdr.pkt_proto = IPPROTO_UDP;
+ m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC);
+ if (flowadv)
+ m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
+
+ im6o = in6p->in6p_moptions;
+ if (im6o != NULL) {
+ IM6O_LOCK(im6o);
+ IM6O_ADDREF_LOCKED(im6o);
+ if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
+ im6o->im6o_multicast_ifp != NULL) {
+ in6p->in6p_last_outifp =
+ im6o->im6o_multicast_ifp;
+ }
+ IM6O_UNLOCK(im6o);
+ }
+
+ in6p->inp_sndinprog_cnt++;
+
+ socket_unlock(so, 0);
+ error = ip6_output(m, optp, &ro, flags, im6o, NULL, &ip6oa);
+ m = NULL;
+ socket_lock(so, 0);
+
+ if (im6o != NULL)
+ IM6O_REMREF(im6o);
+
+ if (error == 0 && nstat_collect) {
+ boolean_t cell, wifi, wired;
+
+ if (in6p->in6p_route.ro_rt != NULL) {
+ cell = IFNET_IS_CELLULAR(in6p->in6p_route.
+ ro_rt->rt_ifp);
+ wifi = (!cell && IFNET_IS_WIFI(in6p->in6p_route.
+ ro_rt->rt_ifp));
+ wired = (!wifi && IFNET_IS_WIRED(in6p->in6p_route.
+ ro_rt->rt_ifp));
+ } else {
+ cell = wifi = wired = FALSE;
+ }
+ INP_ADD_STAT(in6p, cell, wifi, wired, txpackets, 1);
+ INP_ADD_STAT(in6p, cell, wifi, wired, txbytes, ulen);
+ }
+
+ if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
+ adv->code == FADV_SUSPENDED)) {
+ /*
+ * Return an error to indicate
+ * that the packet has been dropped.
+ */
+ error = ENOBUFS;
+ inp_set_fc_state(in6p, adv->code);
+ }
+
+ VERIFY(in6p->inp_sndinprog_cnt > 0);
+ if ( --in6p->inp_sndinprog_cnt == 0)
+ in6p->inp_flags &= ~(INP_FC_FEEDBACK);
+
+ if (ro.ro_rt != NULL) {
+ struct ifnet *outif = ro.ro_rt->rt_ifp;
+
+ so->so_pktheadroom = P2ROUNDUP(
+ sizeof(struct udphdr) +
+ hlen +
+ ifnet_hdrlen(outif) +
+ ifnet_packetpreamblelen(outif),
+ sizeof(u_int32_t));
+ }
+
+ /* Synchronize PCB cached route */
+ in6p_route_copyin(in6p, &ro);
+
+ if (in6p->in6p_route.ro_rt != NULL) {
+ struct rtentry *rt = in6p->in6p_route.ro_rt;
+ struct ifnet *outif;
+
+ if (rt->rt_flags & RTF_MULTICAST)
+ rt = NULL; /* unusable */
+
+ /*
+ * Always discard the cached route for unconnected
+ * socket or if it is a multicast route.
+ */
+ if (rt == NULL)
+ ROUTE_RELEASE(&in6p->in6p_route);
+
+ /*
+ * If the destination route is unicast, update outif
+ * with that of the route interface used by IP.
+ */
+ if (rt != NULL &&
+ (outif = rt->rt_ifp) != in6p->in6p_last_outifp) {
+ in6p->in6p_last_outifp = outif;
+
+ so->so_pktheadroom = P2ROUNDUP(
+ sizeof(struct udphdr) +
+ hlen +
+ ifnet_hdrlen(outif) +
+ ifnet_packetpreamblelen(outif),
+ sizeof(u_int32_t));
+ }
+ } else {
+ ROUTE_RELEASE(&in6p->in6p_route);
+ }
+
+ /*
+ * If output interface was cellular/expensive, and this
+ * socket is denied access to it, generate an event.
+ */
+ if (error != 0 && (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED) &&
+ (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p)))
+ soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED|
+ SO_FILT_HINT_IFDENIED));