Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
240edf1f KS |
2 | * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California. |
3 | * All rights reserved. | |
8ae0e4b4 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
240edf1f | 6 | * |
5548a02f | 7 | * @(#)ns_output.c 7.9 (Berkeley) %G% |
8ae0e4b4 | 8 | */ |
91fdd342 | 9 | |
5548a02f KB |
10 | #include <sys/param.h> |
11 | #include <sys/malloc.h> | |
12 | #include <sys/mbuf.h> | |
13 | #include <sys/errno.h> | |
14 | #include <sys/socket.h> | |
15 | #include <sys/socketvar.h> | |
91fdd342 | 16 | |
5548a02f KB |
17 | #include <net/if.h> |
18 | #include <net/route.h> | |
91fdd342 | 19 | |
5548a02f KB |
20 | #include <netns/ns.h> |
21 | #include <netns/ns_if.h> | |
22 | #include <netns/idp.h> | |
23 | #include <netns/idp_var.h> | |
91fdd342 KS |
24 | |
25 | #ifdef vax | |
5548a02f | 26 | #include <machine/mtpr.h> |
91fdd342 | 27 | #endif |
6bbc5804 KS |
28 | int ns_hold_output = 0; |
29 | int ns_copy_output = 0; | |
30 | int ns_output_cnt = 0; | |
91fdd342 KS |
31 | struct mbuf *ns_lastout; |
32 | ||
91fdd342 KS |
33 | ns_output(m0, ro, flags) |
34 | struct mbuf *m0; | |
35 | struct route *ro; | |
36 | int flags; | |
37 | { | |
38 | register struct idp *idp = mtod(m0, struct idp *); | |
d0c6cd85 | 39 | register struct ifnet *ifp = 0; |
f97be0c9 | 40 | int error = 0; |
91fdd342 KS |
41 | struct route idproute; |
42 | struct sockaddr_ns *dst; | |
43 | extern int idpcksum; | |
44 | ||
5fe5db7d KS |
45 | if (ns_hold_output) { |
46 | if (ns_lastout) { | |
8011f5df | 47 | (void)m_free(ns_lastout); |
91fdd342 | 48 | } |
f97be0c9 | 49 | ns_lastout = m_copy(m0, 0, (int)M_COPYALL); |
91fdd342 | 50 | } |
91fdd342 KS |
51 | /* |
52 | * Route packet. | |
53 | */ | |
54 | if (ro == 0) { | |
55 | ro = &idproute; | |
56 | bzero((caddr_t)ro, sizeof (*ro)); | |
57 | } | |
58 | dst = (struct sockaddr_ns *)&ro->ro_dst; | |
59 | if (ro->ro_rt == 0) { | |
60 | dst->sns_family = AF_NS; | |
4dcdd98e | 61 | dst->sns_len = sizeof (*dst); |
91fdd342 | 62 | dst->sns_addr = idp->idp_dna; |
5fe5db7d | 63 | dst->sns_addr.x_port = 0; |
91fdd342 KS |
64 | /* |
65 | * If routing to interface only, | |
66 | * short circuit routing lookup. | |
67 | */ | |
68 | if (flags & NS_ROUTETOIF) { | |
d0c6cd85 | 69 | struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna); |
b8a7d828 | 70 | |
91fdd342 KS |
71 | if (ia == 0) { |
72 | error = ENETUNREACH; | |
73 | goto bad; | |
74 | } | |
75 | ifp = ia->ia_ifp; | |
76 | goto gotif; | |
77 | } | |
78 | rtalloc(ro); | |
79 | } else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) { | |
80 | /* | |
81 | * The old route has gone away; try for a new one. | |
82 | */ | |
83 | rtfree(ro->ro_rt); | |
e89cd9c7 | 84 | ro->ro_rt = NULL; |
91fdd342 KS |
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)) | |
4dcdd98e | 93 | dst = (struct sockaddr_ns *)ro->ro_rt->rt_gateway; |
91fdd342 KS |
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++; | |
d0c6cd85 KS |
114 | if (ns_copy_output) { |
115 | ns_watch_output(m0, ifp); | |
116 | } | |
fe0fdb83 KS |
117 | error = (*ifp->if_output)(ifp, m0, |
118 | (struct sockaddr *)dst, ro->ro_rt); | |
91fdd342 KS |
119 | goto done; |
120 | } else error = EMSGSIZE; | |
121 | ||
122 | ||
123 | bad: | |
d0c6cd85 KS |
124 | if (ns_copy_output) { |
125 | ns_watch_output(m0, ifp); | |
126 | } | |
91fdd342 KS |
127 | m_freem(m0); |
128 | done: | |
4dcdd98e | 129 | if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt) { |
91fdd342 | 130 | RTFREE(ro->ro_rt); |
4dcdd98e KS |
131 | ro->ro_rt = 0; |
132 | } | |
91fdd342 KS |
133 | return (error); |
134 | } |