1 /* 2 * Copyright (c) 1982 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)ns_output.c 6.3 (Berkeley) 06/08/85 7 */ 8 9 #include "param.h" 10 #include "mbuf.h" 11 #include "errno.h" 12 #include "socket.h" 13 #include "socketvar.h" 14 15 #include "../net/if.h" 16 #include "../net/route.h" 17 18 #include "ns.h" 19 #include "ns_if.h" 20 #include "idp.h" 21 #include "idp_var.h" 22 23 #ifdef vax 24 #include "../vax/mtpr.h" 25 #endif 26 int ns_hold_output = 0; 27 int ns_copy_output = 0; 28 int ns_output_cnt = 0; 29 struct mbuf *ns_lastout; 30 31 ns_output(m0, ro, flags) 32 struct mbuf *m0; 33 struct route *ro; 34 int flags; 35 { 36 register struct idp *idp = mtod(m0, struct idp *); 37 register struct ifnet *ifp; 38 register struct mbuf *m; 39 int len, rlen, off, error = 0; 40 struct route idproute; 41 struct sockaddr_ns *dst; 42 extern int idpcksum; 43 44 if(ns_hold_output) { 45 if(ns_lastout) { 46 m_free(ns_lastout); 47 } 48 ns_lastout = m_copy(m0, 0, M_COPYALL); 49 } 50 if(ns_copy_output) { 51 ns_watch_output(m0); 52 } 53 54 /* 55 * Route packet. 56 */ 57 if (ro == 0) { 58 ro = &idproute; 59 bzero((caddr_t)ro, sizeof (*ro)); 60 } 61 dst = (struct sockaddr_ns *)&ro->ro_dst; 62 if (ro->ro_rt == 0) { 63 dst->sns_family = AF_NS; 64 dst->sns_addr = idp->idp_dna; 65 /* 66 * If routing to interface only, 67 * short circuit routing lookup. 68 */ 69 if (flags & NS_ROUTETOIF) { 70 struct ns_ifaddr *ia; 71 ia = ns_iaonnetof(idp->idp_dna.x_net); 72 if (ia == 0) { 73 error = ENETUNREACH; 74 goto bad; 75 } 76 ifp = ia->ia_ifp; 77 goto gotif; 78 } 79 rtalloc(ro); 80 } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) { 81 /* 82 * The old route has gone away; try for a new one. 83 */ 84 rtfree(ro->ro_rt); 85 rtalloc(ro); 86 } 87 if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) { 88 error = ENETUNREACH; 89 goto bad; 90 } 91 ro->ro_rt->rt_use++; 92 if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST)) 93 dst = (struct sockaddr_ns *)&ro->ro_rt->rt_gateway; 94 gotif: 95 96 /* 97 * Look for multicast addresses and 98 * and verify user is allowed to send 99 * such a packet. 100 */ 101 if (dst->sns_addr.x_host.c_host[0]&1) { 102 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 103 error = EADDRNOTAVAIL; 104 goto bad; 105 } 106 if ((flags & NS_ALLOWBROADCAST) == 0) { 107 error = EACCES; 108 goto bad; 109 } 110 } 111 112 if (htons(idp->idp_len) <= ifp->if_mtu) { 113 ns_output_cnt++; 114 error = (*ifp->if_output)(ifp, m0, (struct sockaddr *)dst); 115 goto done; 116 } else error = EMSGSIZE; 117 118 119 bad: 120 m_freem(m0); 121 done: 122 if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt) 123 RTFREE(ro->ro_rt); 124 return (error); 125 } 126