xref: /csrg-svn/sys/netns/ns_output.c (revision 24226)
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.5 (Berkeley) 08/09/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 			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