0db32e12d651867c80aecb08691062fe57453dc5
[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.6 2003/09/16 05:03:13 hsu 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/netisr.h>
55 #include <net/route.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_atm.h>
59
60 #include <netinet/in.h>
61 #include <netinet/if_atm.h>
62 #include <netinet/if_ether.h> /* XXX: for ETHERTYPE_* */
63 #if defined(INET) || defined(INET6)
64 #include <netinet/in_var.h>
65 #endif
66 #ifdef NATM
67 #include <netproto/natm/natm.h>
68 #endif
69
70 #ifndef ETHERTYPE_IPV6
71 #define ETHERTYPE_IPV6  0x86dd
72 #endif
73
74 #define senderr(e) { error = (e); goto bad;}
75
76 /*
77  * atm_output: ATM output routine
78  *   inputs:
79  *     "ifp" = ATM interface to output to
80  *     "m0" = the packet to output
81  *     "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
82  *     "rt0" = the route to use
83  *   returns: error code   [0 == ok]
84  *
85  *   note: special semantic: if (dst == NULL) then we assume "m" already
86  *              has an atm_pseudohdr on it and just send it directly.
87  *              [for native mode ATM output]   if dst is null, then
88  *              rt0 must also be NULL.
89  */
90
91 int
92 atm_output(ifp, m0, dst, rt0)
93         struct ifnet *ifp;
94         struct mbuf *m0;
95         struct sockaddr *dst;
96         struct rtentry *rt0;
97 {
98         u_int16_t etype = 0;                    /* if using LLC/SNAP */
99         int s, error = 0, sz;
100         struct atm_pseudohdr atmdst, *ad;
101         struct mbuf *m = m0;
102         struct rtentry *rt;
103         struct atmllc *atmllc;
104         struct atmllc *llc_hdr = NULL;
105         u_int32_t atm_flags;
106
107         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
108                 senderr(ENETDOWN);
109
110         /*
111          * check route
112          */
113         if ((rt = rt0) != NULL) {
114
115                 if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */
116                         if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
117                                 rt->rt_refcnt--;
118                         else 
119                                 senderr(EHOSTUNREACH);
120                 }
121
122                 if (rt->rt_flags & RTF_GATEWAY) {
123                         if (rt->rt_gwroute == 0)
124                                 goto lookup;
125                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
126                                 rtfree(rt); rt = rt0;
127                         lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
128                                 if ((rt = rt->rt_gwroute) == 0)
129                                         senderr(EHOSTUNREACH);
130                         }
131                 }
132
133                 /* XXX: put RTF_REJECT code here if doing ATMARP */
134
135         }
136
137         /*
138          * check for non-native ATM traffic   (dst != NULL)
139          */
140         if (dst) {
141                 switch (dst->sa_family) {
142 #if defined(INET) || defined(INET6)
143                 case AF_INET:
144                 case AF_INET6:
145                         if (dst->sa_family == AF_INET6)
146                                 etype = htons(ETHERTYPE_IPV6);
147                         else
148                                 etype = htons(ETHERTYPE_IP);
149                         if (!atmresolve(rt, m, dst, &atmdst)) {
150                                 m = NULL; 
151                                 /* XXX: atmresolve already free'd it */
152                                 senderr(EHOSTUNREACH);
153                                 /* XXX: put ATMARP stuff here */
154                                 /* XXX: watch who frees m on failure */
155                         }
156                         break;
157 #endif /* INET || INET6 */
158
159                 case AF_UNSPEC:
160                         /*
161                          * XXX: bpfwrite. assuming dst contains 12 bytes
162                          * (atm pseudo header (4) + LLC/SNAP (8))
163                          */
164                         bcopy(dst->sa_data, &atmdst, sizeof(atmdst));
165                         llc_hdr = (struct atmllc *)(dst->sa_data + sizeof(atmdst));
166                         break;
167                         
168                 default:
169 #if defined(__NetBSD__) || defined(__OpenBSD__)
170                         printf("%s: can't handle af%d\n", ifp->if_xname, 
171                             dst->sa_family);
172 #elif defined(__FreeBSD__) || defined(__bsdi__)
173                         printf("%s%d: can't handle af%d\n", ifp->if_name, 
174                             ifp->if_unit, dst->sa_family);
175 #endif
176                         senderr(EAFNOSUPPORT);
177                 }
178
179                 /*
180                  * must add atm_pseudohdr to data
181                  */
182                 sz = sizeof(atmdst);
183                 atm_flags = ATM_PH_FLAGS(&atmdst);
184                 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */
185                 M_PREPEND(m, sz, M_DONTWAIT);
186                 if (m == 0)
187                         senderr(ENOBUFS);
188                 ad = mtod(m, struct atm_pseudohdr *);
189                 *ad = atmdst;
190                 if (atm_flags & ATM_PH_LLCSNAP) {
191                         atmllc = (struct atmllc *)(ad + 1);
192                         if (llc_hdr == NULL) {
193                                 bcopy(ATMLLC_HDR, atmllc->llchdr, 
194                                       sizeof(atmllc->llchdr));
195                                 ATM_LLC_SETTYPE(atmllc, etype); 
196                                         /* note: already in network order */
197                         }
198                         else
199                                 bcopy(llc_hdr, atmllc, sizeof(struct atmllc));
200                 }
201         }
202
203         /*
204          * Queue message on interface, and start output if interface
205          * not yet active.
206          */
207         s = splimp();
208         if (IF_QFULL(&ifp->if_snd)) {
209                 IF_DROP(&ifp->if_snd);
210                 splx(s);
211                 senderr(ENOBUFS);
212         }
213         ifp->if_obytes += m->m_pkthdr.len;
214         IF_ENQUEUE(&ifp->if_snd, m);
215         if ((ifp->if_flags & IFF_OACTIVE) == 0)
216                 (*ifp->if_start)(ifp);
217         splx(s);
218         return (error);
219
220 bad:
221         if (m)
222                 m_freem(m);
223         return (error);
224 }
225
226 /*
227  * Process a received ATM packet;
228  * the packet is in the mbuf chain m.
229  */
230 void
231 atm_input(ifp, ah, m, rxhand)
232         struct ifnet *ifp;
233         struct atm_pseudohdr *ah;
234         struct mbuf *m;
235         void *rxhand;
236 {
237         u_int16_t etype = ETHERTYPE_IP; /* default */
238         int isr;
239         int s;
240
241         if ((ifp->if_flags & IFF_UP) == 0) {
242                 m_freem(m);
243                 return;
244         }
245         ifp->if_ibytes += m->m_pkthdr.len;
246
247         if (rxhand) {
248 #ifdef NATM
249                 struct natmpcb *npcb = rxhand;
250                 s = splimp();           /* in case 2 atm cards @ diff lvls */
251                 npcb->npcb_inq++;       /* count # in queue */
252                 splx(s);
253                 isr = NETISR_NATM;
254                 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
255 #else
256                 printf("atm_input: NATM detected but not configured in kernel\n");
257                 m_freem(m);
258                 return;
259 #endif
260         } else {
261                 /*
262                  * handle LLC/SNAP header, if present
263                  */
264                 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
265                         struct atmllc *alc;
266                         if (m->m_len < sizeof(*alc) &&
267                             (m = m_pullup(m, sizeof(*alc))) == 0)
268                                 return; /* failed */
269                         alc = mtod(m, struct atmllc *);
270                         if (bcmp(alc, ATMLLC_HDR, 6)) {
271 #if defined(__NetBSD__) || defined(__OpenBSD__)
272                                 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
273                                        ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
274 #elif defined(__FreeBSD__) || defined(__bsdi__)
275                                 printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
276                                        ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
277 #endif
278                                 m_freem(m);
279                                 return;
280                         }
281                         etype = ATM_LLC_TYPE(alc);
282                         m_adj(m, sizeof(*alc));
283                 }
284
285                 switch (etype) {
286 #ifdef INET
287                 case ETHERTYPE_IP:
288                         isr = NETISR_IP;
289                         break;
290 #endif
291 #ifdef INET6
292                 case ETHERTYPE_IPV6:
293                         isr = NETISR_IPV6;
294                         break;
295 #endif
296                 default:
297                         m_freem(m);
298                         return;
299                 }
300         }
301
302         netisr_dispatch(isr, m);
303 }
304
305 /*
306  * Perform common duties while attaching to interface list
307  */
308 void
309 atm_ifattach(ifp)
310         struct ifnet *ifp;
311 {
312         struct ifaddr *ifa;
313         struct sockaddr_dl *sdl;
314
315         ifp->if_type = IFT_ATM;
316         ifp->if_addrlen = 0;
317         ifp->if_hdrlen = 0;
318         ifp->if_mtu = ATMMTU;
319         ifp->if_output = atm_output;
320         ifp->if_snd.ifq_maxlen = 50;    /* dummy */
321
322 #if defined(__NetBSD__) || defined(__OpenBSD__)
323         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != 0;
324             ifa = TAILQ_NEXT(ifa, ifa_list))
325 #elif defined(__FreeBSD__) && (__FreeBSD__ > 2)
326         for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 
327             ifa = TAILQ_NEXT(ifa, ifa_link))
328 #elif defined(__FreeBSD__) || defined(__bsdi__)
329         for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 
330 #endif
331                 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
332                     sdl->sdl_family == AF_LINK) {
333                         sdl->sdl_type = IFT_ATM;
334                         sdl->sdl_alen = ifp->if_addrlen;
335 #ifdef notyet /* if using ATMARP, store hardware address using the next line */
336                         bcopy(ifp->hw_addr, LLADDR(sdl), ifp->if_addrlen);
337 #endif
338                         break;
339                 }
340
341 }