xref: /original-bsd/sys/netns/ns_output.c (revision 4e9331e4)
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)ns_output.c	7.5 (Berkeley) 04/22/89
18  */
19 
20 #include "param.h"
21 #include "malloc.h"
22 #include "mbuf.h"
23 #include "errno.h"
24 #include "socket.h"
25 #include "socketvar.h"
26 
27 #include "../net/if.h"
28 #include "../net/route.h"
29 
30 #include "ns.h"
31 #include "ns_if.h"
32 #include "idp.h"
33 #include "idp_var.h"
34 
35 #ifdef vax
36 #include "../vax/mtpr.h"
37 #endif
38 int ns_hold_output = 0;
39 int ns_copy_output = 0;
40 int ns_output_cnt = 0;
41 struct mbuf *ns_lastout;
42 
43 ns_output(m0, ro, flags)
44 	struct mbuf *m0;
45 	struct route *ro;
46 	int flags;
47 {
48 	register struct idp *idp = mtod(m0, struct idp *);
49 	register struct ifnet *ifp = 0;
50 	int error = 0;
51 	struct route idproute;
52 	struct sockaddr_ns *dst;
53 	extern int idpcksum;
54 
55 	if (ns_hold_output) {
56 		if (ns_lastout) {
57 			(void)m_free(ns_lastout);
58 		}
59 		ns_lastout = m_copy(m0, 0, (int)M_COPYALL);
60 	}
61 	/*
62 	 * Route packet.
63 	 */
64 	if (ro == 0) {
65 		ro = &idproute;
66 		bzero((caddr_t)ro, sizeof (*ro));
67 	}
68 	dst = (struct sockaddr_ns *)&ro->ro_dst;
69 	if (ro->ro_rt == 0) {
70 		dst->sns_family = AF_NS;
71 		dst->sns_len = sizeof (*dst);
72 		dst->sns_addr = idp->idp_dna;
73 		dst->sns_addr.x_port = 0;
74 		/*
75 		 * If routing to interface only,
76 		 * short circuit routing lookup.
77 		 */
78 		if (flags & NS_ROUTETOIF) {
79 			struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
80 
81 			if (ia == 0) {
82 				error = ENETUNREACH;
83 				goto bad;
84 			}
85 			ifp = ia->ia_ifp;
86 			goto gotif;
87 		}
88 		rtalloc(ro);
89 	} else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
90 		/*
91 		 * The old route has gone away; try for a new one.
92 		 */
93 		rtfree(ro->ro_rt);
94 		ro->ro_rt = NULL;
95 		rtalloc(ro);
96 	}
97 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
98 		error = ENETUNREACH;
99 		goto bad;
100 	}
101 	ro->ro_rt->rt_use++;
102 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
103 		dst = (struct sockaddr_ns *)ro->ro_rt->rt_gateway;
104 gotif:
105 
106 	/*
107 	 * Look for multicast addresses and
108 	 * and verify user is allowed to send
109 	 * such a packet.
110 	 */
111 	if (dst->sns_addr.x_host.c_host[0]&1) {
112 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
113 			error = EADDRNOTAVAIL;
114 			goto bad;
115 		}
116 		if ((flags & NS_ALLOWBROADCAST) == 0) {
117 			error = EACCES;
118 			goto bad;
119 		}
120 	}
121 
122 	if (htons(idp->idp_len) <= ifp->if_mtu) {
123 		ns_output_cnt++;
124 		if (ns_copy_output) {
125 			ns_watch_output(m0, ifp);
126 		}
127 		error = (*ifp->if_output)(ifp, m0, (struct sockaddr *)dst);
128 		goto done;
129 	} else error = EMSGSIZE;
130 
131 
132 bad:
133 	if (ns_copy_output) {
134 		ns_watch_output(m0, ifp);
135 	}
136 	m_freem(m0);
137 done:
138 	if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt) {
139 		RTFREE(ro->ro_rt);
140 		ro->ro_rt = 0;
141 	}
142 	return (error);
143 }
144