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