3 * ===================================
4 * HARP | Host ATM Research Platform
5 * ===================================
8 * This Host ATM Research Platform ("HARP") file (the "Software") is
9 * made available by Network Computing Services, Inc. ("NetworkCS")
10 * "AS IS". NetworkCS does not provide maintenance, improvements or
11 * support of any kind.
13 * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
14 * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
15 * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
16 * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
17 * In no event shall NetworkCS be responsible for any damages, including
18 * but not limited to consequential damages, arising from or relating to
19 * any use of the Software or related support.
21 * Copyright 1994-1998 Network Computing Services, Inc.
23 * Copies of this Software may be made, however, the above copyright
24 * notice must be reproduced on all copies.
26 * @(#) $FreeBSD: src/sys/netatm/atm_if.c,v 1.5 1999/08/28 00:48:35 peter Exp $
27 * @(#) $DragonFly: src/sys/netproto/atm/atm_if.c,v 1.12 2005/11/28 17:13:46 dillon Exp $
34 * ATM interface management
38 #include "kern_include.h"
43 static int atm_physif_ioctl (int, caddr_t, caddr_t);
44 static int atm_netif_rtdel (struct radix_node *, void *);
45 static int atm_if_ioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
46 static int atm_ifparse (char *, char *, int, int *);
51 static int (*atm_ifouttbl[AF_MAX+1])
52 (struct ifnet *, KBuffer *, struct sockaddr *)
57 * Register an ATM physical interface
59 * Each ATM device interface must register itself here upon completing
60 * its internal initialization. This applies to both linked and loaded
61 * device drivers. The interface must be registered before a signalling
62 * manager can be attached.
65 * cup pointer to interface's common unit structure
66 * name pointer to device name string
67 * sdp pointer to interface's stack services
70 * 0 registration successful
71 * errno registration failed - reason indicated
75 atm_physif_register(cup, name, sdp)
78 struct stack_defn *sdp;
83 * See if we need to be initialized
89 * Make sure we're not already registered
91 if (cup->cu_flags & CUF_REGISTER) {
98 * Make sure an interface is only registered once
100 for (pip = atm_interface_head; pip != NULL; pip = pip->pif_next) {
101 if ((cup->cu_unit == pip->pif_unit) &&
102 (strcmp(name, pip->pif_name) == 0)) {
109 * Fill in physical interface parameters
112 pip->pif_name = name;
113 pip->pif_unit = cup->cu_unit;
114 pip->pif_flags = PIF_UP;
115 pip->pif_services = sdp;
116 pip->pif_ioctl = atm_physif_ioctl;
119 * Link in the interface and mark us registered
121 LINK2TAIL(pip, struct atm_pif, atm_interface_head, pif_next);
122 cup->cu_flags |= CUF_REGISTER;
130 * De-register an ATM physical interface
132 * Each ATM interface must de-register itself before downing the interface.
133 * The interface's signalling manager will be detached and any network
134 * interface and VCC control blocks will be freed.
137 * cup pointer to interface's common unit structure
140 * 0 de-registration successful
141 * errno de-registration failed - reason indicated
145 atm_physif_deregister(cup)
148 struct atm_pif *pip = (struct atm_pif *)&cup->cu_pif;
155 * Detach and deregister, if needed
157 if ((cup->cu_flags & CUF_REGISTER)) {
160 * Detach from signalling manager
162 if (pip->pif_sigmgr != NULL) {
163 err = atm_sigmgr_detach(pip);
164 if (err && (err != ENOENT)) {
171 * Make sure signalling manager is detached
173 if (pip->pif_sigmgr != NULL) {
181 UNLINK(pip, struct atm_pif, atm_interface_head, pif_next);
183 cup->cu_flags &= ~CUF_REGISTER;
187 * Free all of our network interfaces
189 atm_physif_freenifs(pip);
192 * Free unit's vcc information
199 cup->cu_vcc = (Cmn_vcc *)NULL;
208 * Free all network interfaces on a physical interface
211 * pip pointer to physical interface structure
218 atm_physif_freenifs(pip)
221 struct atm_nif *nip = pip->pif_nif;
227 * atm_nif_detach zeros pointers - save so we can
230 struct atm_nif *nipp = nip->nif_pnext;
233 * Clean up network i/f trails
235 atm_nif_detach ( nip );
236 atm_free ((caddr_t)nip);
239 pip->pif_nif = (struct atm_nif *)NULL;
248 * Handle physical interface ioctl's
250 * See <netatm/atm_ioctl.h> for definitions.
252 * Called from a critical section.
255 * code Ioctl function (sub)code
256 * data Data block. On input contains command,
257 * on output, contains results
258 * arg Optional code specific arguments
261 * 0 Request processed successfully
262 * errno Request failed - reason code
266 atm_physif_ioctl(code, data, arg)
271 struct atminfreq *aip = (struct atminfreq *)data;
272 struct atmsetreq *asr = (struct atmsetreq *)data;
280 caddr_t buf = aip->air_buf_addr;
281 struct air_phy_stat_rsp *apsp;
282 struct air_int_rsp apr;
283 struct air_netif_rsp anr;
284 struct air_cfg_rsp acr;
285 int count, len, buf_len = aip->air_buf_len;
287 char ifname[2*IFNAMSIZ];
288 struct in_ifaddr *ia;
289 struct sockaddr_dl *sdl;
291 switch ( aip->air_opcode ) {
295 * Get physical interface information
297 aip = (struct atminfreq *)data;
298 pip = (struct atm_pif *)arg;
301 * Make sure there's room in user buffer
303 if (aip->air_buf_len < sizeof(apr)) {
309 * Fill in info to be returned
311 KM_ZERO((caddr_t)&apr, sizeof(apr));
312 smp = pip->pif_sigmgr;
313 sip = pip->pif_siginst;
314 (void) snprintf(apr.anp_intf, sizeof(apr.anp_intf),
315 "%s%d", pip->pif_name, pip->pif_unit );
318 strcpy(apr.anp_nif_pref, /* XXX: strings */
319 pip->pif_nif->nif_if.if_dname);
324 nip = nip->nif_pnext;
328 ATM_ADDR_COPY(&sip->si_addr, &apr.anp_addr);
329 ATM_ADDR_COPY(&sip->si_subaddr, &apr.anp_subaddr);
330 apr.anp_sig_proto = smp->sm_proto;
331 apr.anp_sig_state = sip->si_state;
335 * Copy data to user buffer
337 err = copyout((caddr_t)&apr, aip->air_buf_addr, sizeof(apr));
342 * Update buffer pointer/count
344 aip->air_buf_addr += sizeof(apr);
345 aip->air_buf_len -= sizeof(apr);
350 * Get network interface information
352 aip = (struct atminfreq *)data;
353 nip = (struct atm_nif *)arg;
358 * Make sure there's room in user buffer
360 if (aip->air_buf_len < sizeof(anr)) {
366 * Fill in info to be returned
368 KM_ZERO((caddr_t)&anr, sizeof(anr));
369 (void) snprintf(anr.anp_intf, sizeof(anr.anp_intf),
370 "%s", ifp->if_xname);
373 anr.anp_proto_addr = *ia->ia_ifa.ifa_addr;
375 (void) snprintf(anr.anp_phy_intf, sizeof(anr.anp_phy_intf),
376 "%s%d", pip->pif_name, pip->pif_unit);
379 * Copy data to user buffer
381 err = copyout((caddr_t)&anr, aip->air_buf_addr, sizeof(anr));
386 * Update buffer pointer/count
388 aip->air_buf_addr += sizeof(anr);
389 aip->air_buf_len -= sizeof(anr);
394 * Get per interface statistics
396 pip = (struct atm_pif *)arg;
399 snprintf ( ifname, sizeof(ifname),
400 "%s%d", pip->pif_name, pip->pif_unit );
403 * Cast response into users buffer
405 apsp = (struct air_phy_stat_rsp *)buf;
410 len = sizeof ( struct air_phy_stat_rsp );
415 * Copy interface name into response
417 if ((err = copyout ( ifname, apsp->app_intf, IFNAMSIZ)) != 0)
423 if ((err = copyout(&pip->pif_ipdus, &apsp->app_ipdus,
424 len - sizeof(apsp->app_intf))) != 0)
428 * Adjust buffer elements
433 aip->air_buf_addr = buf;
434 aip->air_buf_len = buf_len;
439 * Set NIF - allow user to configure 1 or more logical
440 * interfaces per physical interface.
444 * Get pointer to physical interface structure from
447 pip = (struct atm_pif *)arg;
448 cup = (Cmn_unit *)pip;
451 * Sanity check - are we already connected to something?
453 if ( pip->pif_sigmgr )
460 * Free any previously allocated NIFs
462 atm_physif_freenifs(pip);
465 * Add list of interfaces
467 for ( count = 0; count < asr->asr_nif_cnt; count++ )
469 nip = (struct atm_nif *)atm_allocate(cup->cu_nif_pool);
473 * Destroy any successful nifs
475 atm_physif_freenifs(pip);
483 strcpy ( nip->nif_name, asr->asr_nif_pref );
484 nip->nif_sel = count;
486 if_initname(ifp, nip->nif_name, count);
487 ifp->if_mtu = ATM_NIF_MTU;
488 ifp->if_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
489 ifp->if_output = atm_ifoutput;
490 ifp->if_ioctl = atm_if_ioctl;
491 ifp->if_snd.ifq_maxlen = ifqmaxlen;
493 * Set if_type and if_baudrate
495 ifp->if_type = IFT_ATM;
496 switch ( cup->cu_config.ac_media ) {
498 ifp->if_baudrate = 100000000;
501 ifp->if_baudrate = 140000000;
506 ifp->if_baudrate = 155000000;
509 panic("AIOCF_SETNIF: unknown media");
512 if ((err = atm_nif_attach(nip)) != 0) {
513 atm_free ( (caddr_t)nip );
516 * Destroy any successful nifs
518 atm_physif_freenifs(pip);
522 * Set macaddr in <Link> address
525 sdl = IF_LLSOCKADDR(ifp);
526 sdl->sdl_type = IFT_ETHER;
527 sdl->sdl_alen = ifp->if_addrlen;
528 bcopy ( (caddr_t)&cup->cu_config.ac_macaddr,
529 LLADDR(sdl), ifp->if_addrlen );
535 * Get adapter configuration information
537 aip = (struct atminfreq *)data;
538 pip = (struct atm_pif *)arg;
539 cup = (Cmn_unit *)pip;
540 acp = &cup->cu_config;
543 * Make sure there's room in user buffer
545 if (aip->air_buf_len < sizeof(acr)) {
551 * Fill in info to be returned
553 KM_ZERO((caddr_t)&acr, sizeof(acr));
554 (void) snprintf(acr.acp_intf, sizeof(acr.acp_intf),
555 "%s%d", pip->pif_name, pip->pif_unit);
556 KM_COPY((caddr_t)acp, (caddr_t)&acr.acp_cfg,
560 * Copy data to user buffer
562 err = copyout((caddr_t)&acr, aip->air_buf_addr,
568 * Update buffer pointer/count
570 aip->air_buf_addr += sizeof(acr);
571 aip->air_buf_len -= sizeof(acr);
576 * Pass off to device-specific handler
578 cup = (Cmn_unit *)arg;
582 err = (*cup->cu_ioctl)(code, data, arg);
594 * Register a Network Convergence Module
596 * Each ATM network convergence module must register itself here before
597 * it will receive network interface status notifications.
600 * ncp pointer to network convergence definition structure
603 * 0 registration successful
604 * errno registration failed - reason indicated
608 atm_netconv_register(ncp)
615 * See if we need to be initialized
621 * Validate protocol family
623 if (ncp->ncm_family > AF_MAX) {
629 * Ensure no duplicates
631 for (tdp = atm_netconv_head; tdp != NULL; tdp = tdp->ncm_next) {
632 if (tdp->ncm_family == ncp->ncm_family) {
641 LINK2TAIL(ncp, struct atm_ncm, atm_netconv_head, ncm_next);
644 * Add new interface output function
646 atm_ifouttbl[ncp->ncm_family] = ncp->ncm_ifoutput;
654 * De-register an ATM Network Convergence Module
656 * Each ATM network convergence provider must de-register its registered
657 * service(s) before terminating. Specifically, loaded kernel modules
658 * must de-register their services before unloading themselves.
661 * ncp pointer to network convergence definition structure
664 * 0 de-registration successful
665 * errno de-registration failed - reason indicated
669 atm_netconv_deregister(ncp)
677 * Remove module from list
679 UNLINKF(ncp, struct atm_ncm, atm_netconv_head, ncm_next, found);
687 * Remove module's interface output function
689 atm_ifouttbl[ncp->ncm_family] = NULL;
697 * Attach an ATM Network Interface
699 * Before an ATM network interface can be used by the system, the owning
700 * device interface must attach the network interface using this function.
701 * The physical interface for this network interface must have been previously
702 * registered (using atm_interface_register). The network interface will be
703 * added to the kernel's interface list and to the physical interface's list.
704 * The caller is responsible for initializing the control block fields.
707 * nip pointer to atm network interface control block
710 * 0 attach successful
711 * errno attach failed - reason indicated
715 atm_nif_attach(struct atm_nif *nip, lwkt_serialize_t serializer)
717 struct atm_pif *pip, *pip2;
727 * Verify physical interface is registered
729 for (pip2 = atm_interface_head; pip2 != NULL; pip2 = pip2->pif_next) {
733 if ((pip == NULL) || (pip2 == NULL)) {
739 * Add to system interface list
741 if_attach(ifp, serializer);
744 * Add to physical interface list
746 LINK2TAIL(nip, struct atm_nif, pip->pif_nif, nif_pnext);
749 * Notify network convergence modules of new network i/f
751 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
754 err = (*ncp->ncm_stat)(NCM_ATTACH, nip, 0);
768 * Detach an ATM Network Interface
770 * Before an ATM network interface control block can be freed, all kernel
771 * references to/from this block must be released. This function will delete
772 * all routing references to the interface and free all interface addresses
773 * for the interface. The network interface will then be removed from the
774 * kernel's interface list and from the owning physical interface's list.
775 * The caller is responsible for free'ing the control block.
778 * nip pointer to atm network interface control block
790 struct ifnet *ifp = &nip->nif_if;
792 struct in_ifaddr *ia;
793 struct radix_node_head *rnh;
798 * Notify convergence modules of network i/f demise
800 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
801 (void) (*ncp->ncm_stat)(NCM_DETACH, nip, 0);
805 * Mark interface down
810 * Free all interface routes and addresses
817 /* Delete interface route */
820 /* Remove interface address from queues */
822 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
823 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
825 /* Free interface address */
830 * Delete all remaining routes using this interface
831 * Unfortuneatly the only way to do this is to slog through
832 * the entire routing table looking for routes which point
833 * to this interface...oh well...
835 for (i = 1; i <= AF_MAX; i++) {
836 if ((rnh = rt_tables[i]) == NULL)
838 (void) rnh->rnh_walktree(rnh, atm_netif_rtdel, ifp);
842 * Remove from system interface list (ie. if_detach())
844 TAILQ_REMOVE(&ifnet, ifp, if_link);
847 * Remove from physical interface list
849 UNLINK(nip, struct atm_nif, nip->nif_pif->pif_nif, nif_pnext);
856 * Delete Routes for a Network Interface
858 * Called for each routing entry via the rnh->rnh_walktree() call above
859 * to delete all route entries referencing a detaching network interface.
862 * rn pointer to node in the routing table
863 * arg argument passed to rnh->rnh_walktree() - detaching interface
867 * errno failed - reason indicated
871 atm_netif_rtdel(rn, arg)
872 struct radix_node *rn;
875 struct rtentry *rt = (struct rtentry *)rn;
876 struct ifnet *ifp = arg;
879 if (rt->rt_ifp == ifp) {
882 * Protect (sorta) against walktree recursion problems
885 if ((rt->rt_flags & RTF_UP) == 0)
888 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
889 rt_mask(rt), rt->rt_flags,
890 (struct rtentry **) NULL);
892 log(LOG_WARNING, "atm_netif_rtdel: error %d\n", err);
901 * Set an ATM Network Interface address
903 * This is called from a device interface when processing an SIOCSIFADDR
904 * ioctl request. We just notify all convergence modules of the new address
905 * and hope everyone has non-overlapping interests, since if someone reports
906 * an error we don't go back and tell everyone to undo the change.
909 * nip pointer to atm network interface control block
910 * ifa pointer to new interface address
914 * errno set failed - reason indicated
918 atm_nif_setaddr(nip, ifa)
927 * Notify convergence modules of network i/f change
929 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
930 err = (*ncp->ncm_stat)(NCM_SETADDR, nip, (int)ifa);
941 * ATM Interface Packet Output
943 * All ATM network interfaces must have their ifnet if_output address set to
944 * this function. Since no existing network layer code is to be modified
945 * for ATM support, this function serves as the hook to allow network output
946 * packets to be assigned to their proper outbound VCC. Each network address
947 * family which is to be supported over ATM must be assigned an output
948 * packet processing function via atm_netconv_register().
951 * ifp pointer to ifnet structure
952 * m pointer to packet buffer chain to be output
953 * dst pointer to packet's network destination address
956 * 0 packet queued to interface
957 * errno output failed - reason indicated
961 atm_ifoutput(ifp, m, dst, rt)
964 struct sockaddr *dst;
967 u_short fam = dst->sa_family;
968 int (*func)(struct ifnet *, KBuffer *,
972 * Validate address family
976 return (EAFNOSUPPORT);
980 * Hand packet off for dst-to-VCC mapping
982 func = atm_ifouttbl[fam];
985 return (EAFNOSUPPORT);
987 return ((*func)(ifp, m, dst));
992 * Handle interface ioctl requests.
995 * ifp pointer to network interface structure
997 * data arguments to/from ioctl
1003 atm_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1005 struct ifreq *ifr = (struct ifreq *)data;
1006 struct atm_nif *nip = (struct atm_nif *)ifp;
1013 KM_COPY ( (caddr_t)&(nip->nif_pif->pif_macaddr),
1014 (caddr_t)ifr->ifr_addr.sa_data,
1015 sizeof(struct mac_addr) );
1019 error = atm_nif_setaddr ( nip, (struct ifaddr *)data);
1020 ifp->if_flags |= IFF_UP | IFF_RUNNING | IFF_BROADCAST;
1024 *(short *)data = ifp->if_flags;
1041 * Parse interface name
1043 * Parses an interface name string into a name and a unit component.
1046 * name pointer to interface name string
1047 * namep address to store interface name
1048 * size size available at namep
1049 * unitp address to store interface unit number
1057 atm_ifparse(name, namep, size, unitp)
1064 int len = 0, unit = 0;
1067 * Separate supplied string into name and unit parts.
1072 if (*cp >= '0' && *cp <= '9')
1079 while (*cp && *cp >= '0' && *cp <= '9')
1080 unit = 10 * unit + *cp++ - '0';
1089 * Locate ATM physical interface via name
1091 * Uses the supplied interface name string to locate a registered
1092 * ATM physical interface.
1095 * name pointer to interface name string
1098 * 0 interface not found
1099 * else pointer to atm physical interface structure
1106 struct atm_pif *pip;
1113 if (atm_ifparse(name, n, sizeof(n), &unit))
1114 return ((struct atm_pif *)0);
1117 * Look for the physical interface
1119 for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1120 if ((pip->pif_unit == unit) && (strcmp(pip->pif_name, n) == 0))
1129 * Locate ATM network interface via name
1131 * Uses the supplied interface name string to locate an ATM network interface.
1134 * name pointer to interface name string
1137 * 0 interface not found
1138 * else pointer to atm network interface structure
1145 struct atm_pif *pip;
1146 struct atm_nif *nip;
1153 if (atm_ifparse(name, n, sizeof(n), &unit))
1154 return ((struct atm_nif *)0);
1157 * Search thru each physical interface
1159 for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1161 * Looking for network interface
1163 for (nip = pip->pif_nif; nip; nip = nip->nif_pnext) {
1164 struct ifnet *ifp = (struct ifnet *)nip;
1165 if ((ifp->if_dunit == unit) &&
1166 (strcmp(ifp->if_dname, n) == 0))