Let the allocation of registers be done by compilers nowadays. The average
[dragonfly.git] / sys / net / if_atmsubr.c
1 /*      $NetBSD: if_atmsubr.c,v 1.10 1997/03/11 23:19:51 chuck Exp $       */
2
3 /*
4  *
5  * Copyright (c) 1996 Charles D. Cranor and Washington University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor and 
19  *      Washington University.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/net/if_atmsubr.c,v 1.10.2.1 2001/03/06 00:29:26 obrien Exp $
35  * $DragonFly: src/sys/net/if_atmsubr.c,v 1.10 2004/07/23 07:16:30 joerg Exp $
36  */
37
38 /*
39  * if_atmsubr.c
40  */
41
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_natm.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/errno.h>
52
53 #include <net/if.h>
54 #include <net/bpf.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/if_atm.h>
60
61 #include <netinet/in.h>
62 #include <netinet/if_atm.h>
63 #include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in_var.h>
66 #endif
67 #ifdef NATM
68 #include <netproto/natm/natm.h>
69 #endif
70
71 #ifndef ETHERTYPE_IPV6
72 #define ETHERTYPE_IPV6  0x86dd
73 #endif
74
75 #define senderr(e) { error = (e); goto bad;}
76
77 /*
78  * atm_output: ATM output routine
79  *   inputs:
80  *     "ifp" = ATM interface to output to
81  *     "m0" = the packet to output
82  *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
83  *     "rt0" = the route to use
84  *   returns: error code   [0 == ok]
85  *
86  *   note: special semantic: if (dst == NULL) then we assume "m" already
87  *              has an atm_pseudohdr on it and just send it directly.
88  *              [for native mode ATM output]   if dst is null, then
89  *              rt0 must also be NULL.
90  */
91
92 int
93 atm_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
94            struct rtentry *rt0)
95 {
96         u_int16_t etype = 0;                    /* if using LLC/SNAP */
97         int s, error = 0, sz;
98         struct atm_pseudohdr atmdst, *ad;
99         struct mbuf *m = m0;
100         struct rtentry *rt;
101         struct atmllc *atmllc;
102         struct atmllc *llc_hdr = NULL;
103         u_int32_t atm_flags;
104
105         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
106                 senderr(ENETDOWN);
107
108         /*
109          * check route
110          */
111         if ((rt = rt0) != NULL) {
112
113                 if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */
114                         if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
115                                 rt->rt_refcnt--;
116                         else 
117                                 senderr(EHOSTUNREACH);
118                 }
119
120                 if (rt->rt_flags & RTF_GATEWAY) {
121                         if (rt->rt_gwroute == 0)
122                                 goto lookup;
123                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
124                                 rtfree(rt); rt = rt0;
125                         lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
126                                 if ((rt = rt->rt_gwroute) == 0)
127                                         senderr(EHOSTUNREACH);
128                         }
129                 }
130
131                 /* XXX: put RTF_REJECT code here if doing ATMARP */
132
133         }
134
135         /*
136          * check for non-native ATM traffic   (dst != NULL)
137          */
138         if (dst) {
139                 switch (dst->sa_family) {
140 #if defined(INET) || defined(INET6)
141                 case AF_INET:
142                 case AF_INET6:
143                         if (dst->sa_family == AF_INET6)
144                                 etype = htons(ETHERTYPE_IPV6);
145                         else
146                                 etype = htons(ETHERTYPE_IP);
147                         if (!atmresolve(rt, m, dst, &atmdst)) {
148                                 m = NULL; 
149                                 /* XXX: atmresolve already free'd it */
150                                 senderr(EHOSTUNREACH);
151                                 /* XXX: put ATMARP stuff here */
152                                 /* XXX: watch who frees m on failure */
153                         }
154                         break;
155 #endif /* INET || INET6 */
156
157                 case AF_UNSPEC:
158                         /*
159                          * XXX: bpfwrite. assuming dst contains 12 bytes
160                          * (atm pseudo header (4) + LLC/SNAP (8))
161                          */
162                         bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
163                         llc_hdr = (struct atmllc *)(dst->sa_data + sizeof(atmdst));
164                         break;
165                         
166                 default:
167 #if defined(__NetBSD__) || defined(__OpenBSD__)
168                         printf("%s: can't handle af%d\n", ifp->if_xname, 
169                             dst->sa_family);
170 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__bsdi__)
171                         printf("%s: can't handle af%d\n", ifp->if_xname, 
172                             dst->sa_family);
173 #endif
174                         senderr(EAFNOSUPPORT);
175                 }
176
177                 /*
178                  * must add atm_pseudohdr to data
179                  */
180                 sz = sizeof(atmdst);
181                 atm_flags = ATM_PH_FLAGS(&atmdst);
182                 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */
183                 M_PREPEND(m, sz, MB_DONTWAIT);
184                 if (m == 0)
185                         senderr(ENOBUFS);
186                 ad = mtod(m, struct atm_pseudohdr *);
187                 *ad = atmdst;
188                 if (atm_flags & ATM_PH_LLCSNAP) {
189                         atmllc = (struct atmllc *)(ad + 1);
190                         if (llc_hdr == NULL) {
191                                 bcopy(ATMLLC_HDR, atmllc->llchdr, 
192                                       sizeof(atmllc->llchdr));
193                                 ATM_LLC_SETTYPE(atmllc, etype); 
194                                         /* note: already in network order */
195                         }
196                         else
197                                 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
198                 }
199         }
200
201         /*
202          * Queue message on interface, and start output if interface
203          * not yet active.
204          */
205         s = splimp();
206         if (IF_QFULL(&ifp->if_snd)) {
207                 IF_DROP(&ifp->if_snd);
208                 splx(s);
209                 senderr(ENOBUFS);
210         }
211         ifp->if_obytes += m->m_pkthdr.len;
212         IF_ENQUEUE(&ifp->if_snd, m);
213         if ((ifp->if_flags & IFF_OACTIVE) == 0)
214                 (*ifp->if_start)(ifp);
215         splx(s);
216         return (error);
217
218 bad:
219         if (m)
220                 m_freem(m);
221         return (error);
222 }
223
224 /*
225  * Process a received ATM packet;
226  * the packet is in the mbuf chain m.
227  */
228 void
229 atm_input(ifp, ah, m, rxhand)
230         struct ifnet *ifp;
231         struct atm_pseudohdr *ah;
232         struct mbuf *m;
233         void *rxhand;
234 {
235         u_int16_t etype = ETHERTYPE_IP; /* default */
236         int isr;
237         int s;
238
239         if ((ifp->if_flags & IFF_UP) == 0) {
240                 m_freem(m);
241                 return;
242         }
243         ifp->if_ibytes += m->m_pkthdr.len;
244
245         if (rxhand) {
246 #ifdef NATM
247                 struct natmpcb *npcb = rxhand;
248                 s = splimp();           /* in case 2 atm cards @ diff lvls */
249                 npcb->npcb_inq++;       /* count # in queue */
250                 splx(s);
251                 isr = NETISR_NATM;
252                 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
253 #else
254                 printf("atm_input: NATM detected but not configured in kernel\n");
255                 m_freem(m);
256                 return;
257 #endif
258         } else {
259                 /*
260                  * handle LLC/SNAP header, if present
261                  */
262                 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
263                         struct atmllc *alc;
264                         if (m->m_len < sizeof(*alc) &&
265                             (m = m_pullup(m, sizeof(*alc))) == 0)
266                                 return; /* failed */
267                         alc = mtod(m, struct atmllc *);
268                         if (bcmp(alc, ATMLLC_HDR, 6)) {
269 #if defined(__NetBSD__) || defined(__OpenBSD__)
270                                 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
271                                        ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
272 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__bsdi__)
273                                 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
274                                        ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
275 #endif
276                                 m_freem(m);
277                                 return;
278                         }
279                         etype = ATM_LLC_TYPE(alc);
280                         m_adj(m, sizeof(*alc));
281                 }
282
283                 switch (etype) {
284 #ifdef INET
285                 case ETHERTYPE_IP:
286                         isr = NETISR_IP;
287                         break;
288 #endif
289 #ifdef INET6
290                 case ETHERTYPE_IPV6:
291                         isr = NETISR_IPV6;
292                         break;
293 #endif
294                 default:
295                         m_freem(m);
296                         return;
297                 }
298         }
299
300         netisr_dispatch(isr, m);
301 }
302
303 /*
304  * Perform common duties while attaching to interface list
305  */
306 void
307 atm_ifattach(ifp)
308         struct ifnet *ifp;
309 {
310         struct ifaddr *ifa;
311         struct sockaddr_dl *sdl;
312
313         ifp->if_type = IFT_ATM;
314         ifp->if_addrlen = 0;
315         ifp->if_hdrlen = 0;
316         ifp->if_mtu = ATMMTU;
317 #if 0 /* XXX */
318         ifp->if_input = atm_input;
319 #endif
320         ifp->if_output = atm_output;
321         ifp->if_snd.ifq_maxlen = 50;    /* dummy */
322         if_attach(ifp);
323
324 #if defined(__NetBSD__) || defined(__OpenBSD__)
325         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != 0;
326             ifa = TAILQ_NEXT(ifa, ifa_list))
327 #elif defined(__DragonFly__) || (defined(__FreeBSD__) && (__FreeBSD__ > 2))
328         for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 
329             ifa = TAILQ_NEXT(ifa, ifa_link))
330 #elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__bsdi__)
331         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 
332 #endif
333                 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
334                     sdl->sdl_family == AF_LINK) {
335                         sdl->sdl_type = IFT_ATM;
336                         sdl->sdl_alen = ifp->if_addrlen;
337 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
338                         bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
339 #endif
340                         break;
341                 }
342         bpfattach(ifp, DLT_ATM_RFC1483, sizeof(struct atmllc));
343 }