xref: /csrg-svn/sys/netns/ns_output.c (revision 25043)
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.7 (Berkeley) 09/26/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 = 0;
38 	int error = 0;
39 	struct route idproute;
40 	struct sockaddr_ns *dst;
41 	extern int idpcksum;
42 
43 	if (ns_hold_output) {
44 		if (ns_lastout) {
45 			m_free(ns_lastout);
46 		}
47 		ns_lastout = m_copy(m0, 0, (int)M_COPYALL);
48 	}
49 	/*
50 	 * Route packet.
51 	 */
52 	if (ro == 0) {
53 		ro = &idproute;
54 		bzero((caddr_t)ro, sizeof (*ro));
55 	}
56 	dst = (struct sockaddr_ns *)&ro->ro_dst;
57 	if (ro->ro_rt == 0) {
58 		dst->sns_family = AF_NS;
59 		dst->sns_addr = idp->idp_dna;
60 		dst->sns_addr.x_port = 0;
61 		/*
62 		 * If routing to interface only,
63 		 * short circuit routing lookup.
64 		 */
65 		if (flags & NS_ROUTETOIF) {
66 			struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
67 
68 			if (ia == 0) {
69 				error = ENETUNREACH;
70 				goto bad;
71 			}
72 			ifp = ia->ia_ifp;
73 			goto gotif;
74 		}
75 		rtalloc(ro);
76 	} else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
77 		/*
78 		 * The old route has gone away; try for a new one.
79 		 */
80 		rtfree(ro->ro_rt);
81 		rtalloc(ro);
82 	}
83 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
84 		error = ENETUNREACH;
85 		goto bad;
86 	}
87 	ro->ro_rt->rt_use++;
88 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
89 		dst = (struct sockaddr_ns *)&ro->ro_rt->rt_gateway;
90 gotif:
91 
92 	/*
93 	 * Look for multicast addresses and
94 	 * and verify user is allowed to send
95 	 * such a packet.
96 	 */
97 	if (dst->sns_addr.x_host.c_host[0]&1) {
98 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
99 			error = EADDRNOTAVAIL;
100 			goto bad;
101 		}
102 		if ((flags & NS_ALLOWBROADCAST) == 0) {
103 			error = EACCES;
104 			goto bad;
105 		}
106 	}
107 
108 	if (htons(idp->idp_len) <= ifp->if_mtu) {
109 		ns_output_cnt++;
110 		if (ns_copy_output) {
111 			ns_watch_output(m0, ifp);
112 		}
113 		error = (*ifp->if_output)(ifp, m0, (struct sockaddr *)dst);
114 		goto done;
115 	} else error = EMSGSIZE;
116 
117 
118 bad:
119 	if (ns_copy_output) {
120 		ns_watch_output(m0, ifp);
121 	}
122 	m_freem(m0);
123 done:
124 	if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt)
125 		RTFREE(ro->ro_rt);
126 	return (error);
127 }
128