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.9 2005/02/01 00:51:50 joerg 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;
84 * See if we need to be initialized
90 * Make sure we're not already registered
92 if (cup->cu_flags & CUF_REGISTER) {
99 * Make sure an interface is only registered once
101 for (pip = atm_interface_head; pip != NULL; pip = pip->pif_next) {
102 if ((cup->cu_unit == pip->pif_unit) &&
103 (strcmp(name, pip->pif_name) == 0)) {
110 * Fill in physical interface parameters
113 pip->pif_name = name;
114 pip->pif_unit = cup->cu_unit;
115 pip->pif_flags = PIF_UP;
116 pip->pif_services = sdp;
117 pip->pif_ioctl = atm_physif_ioctl;
120 * Link in the interface and mark us registered
122 LINK2TAIL(pip, struct atm_pif, atm_interface_head, pif_next);
123 cup->cu_flags |= CUF_REGISTER;
131 * De-register an ATM physical interface
133 * Each ATM interface must de-register itself before downing the interface.
134 * The interface's signalling manager will be detached and any network
135 * interface and VCC control blocks will be freed.
138 * cup pointer to interface's common unit structure
141 * 0 de-registration successful
142 * errno de-registration failed - reason indicated
146 atm_physif_deregister(cup)
149 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.
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];
289 struct in_ifaddr *ia;
290 struct sockaddr_dl *sdl;
292 switch ( aip->air_opcode ) {
296 * Get physical interface information
298 aip = (struct atminfreq *)data;
299 pip = (struct atm_pif *)arg;
302 * Make sure there's room in user buffer
304 if (aip->air_buf_len < sizeof(apr)) {
310 * Fill in info to be returned
312 KM_ZERO((caddr_t)&apr, sizeof(apr));
313 smp = pip->pif_sigmgr;
314 sip = pip->pif_siginst;
315 (void) snprintf(apr.anp_intf, sizeof(apr.anp_intf),
316 "%s%d", pip->pif_name, pip->pif_unit );
319 strcpy(apr.anp_nif_pref, /* XXX: strings */
320 pip->pif_nif->nif_if.if_dname);
325 nip = nip->nif_pnext;
329 ATM_ADDR_COPY(&sip->si_addr, &apr.anp_addr);
330 ATM_ADDR_COPY(&sip->si_subaddr, &apr.anp_subaddr);
331 apr.anp_sig_proto = smp->sm_proto;
332 apr.anp_sig_state = sip->si_state;
336 * Copy data to user buffer
338 err = copyout((caddr_t)&apr, aip->air_buf_addr, sizeof(apr));
343 * Update buffer pointer/count
345 aip->air_buf_addr += sizeof(apr);
346 aip->air_buf_len -= sizeof(apr);
351 * Get network interface information
353 aip = (struct atminfreq *)data;
354 nip = (struct atm_nif *)arg;
359 * Make sure there's room in user buffer
361 if (aip->air_buf_len < sizeof(anr)) {
367 * Fill in info to be returned
369 KM_ZERO((caddr_t)&anr, sizeof(anr));
370 (void) snprintf(anr.anp_intf, sizeof(anr.anp_intf),
371 "%s", ifp->if_xname);
374 anr.anp_proto_addr = *ia->ia_ifa.ifa_addr;
376 (void) snprintf(anr.anp_phy_intf, sizeof(anr.anp_phy_intf),
377 "%s%d", pip->pif_name, pip->pif_unit);
380 * Copy data to user buffer
382 err = copyout((caddr_t)&anr, aip->air_buf_addr, sizeof(anr));
387 * Update buffer pointer/count
389 aip->air_buf_addr += sizeof(anr);
390 aip->air_buf_len -= sizeof(anr);
395 * Get per interface statistics
397 pip = (struct atm_pif *)arg;
400 snprintf ( ifname, sizeof(ifname),
401 "%s%d", pip->pif_name, pip->pif_unit );
404 * Cast response into users buffer
406 apsp = (struct air_phy_stat_rsp *)buf;
411 len = sizeof ( struct air_phy_stat_rsp );
416 * Copy interface name into response
418 if ((err = copyout ( ifname, apsp->app_intf, IFNAMSIZ)) != 0)
424 if ((err = copyout(&pip->pif_ipdus, &apsp->app_ipdus,
425 len - sizeof(apsp->app_intf))) != 0)
429 * Adjust buffer elements
434 aip->air_buf_addr = buf;
435 aip->air_buf_len = buf_len;
440 * Set NIF - allow user to configure 1 or more logical
441 * interfaces per physical interface.
445 * Get pointer to physical interface structure from
448 pip = (struct atm_pif *)arg;
449 cup = (Cmn_unit *)pip;
452 * Sanity check - are we already connected to something?
454 if ( pip->pif_sigmgr )
461 * Free any previously allocated NIFs
463 atm_physif_freenifs(pip);
466 * Add list of interfaces
468 for ( count = 0; count < asr->asr_nif_cnt; count++ )
470 nip = (struct atm_nif *)atm_allocate(cup->cu_nif_pool);
474 * Destroy any successful nifs
476 atm_physif_freenifs(pip);
484 strcpy ( nip->nif_name, asr->asr_nif_pref );
485 nip->nif_sel = count;
487 if_initname(ifp, nip->nif_name, count);
488 ifp->if_mtu = ATM_NIF_MTU;
489 ifp->if_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
490 ifp->if_output = atm_ifoutput;
491 ifp->if_ioctl = atm_if_ioctl;
492 ifp->if_snd.ifq_maxlen = ifqmaxlen;
494 * Set if_type and if_baudrate
496 ifp->if_type = IFT_ATM;
497 switch ( cup->cu_config.ac_media ) {
499 ifp->if_baudrate = 100000000;
502 ifp->if_baudrate = 140000000;
507 ifp->if_baudrate = 155000000;
510 panic("AIOCF_SETNIF: unknown media");
513 if ((err = atm_nif_attach(nip)) != 0) {
514 atm_free ( (caddr_t)nip );
517 * Destroy any successful nifs
519 atm_physif_freenifs(pip);
523 * Set macaddr in <Link> address
526 ifa = ifnet_addrs[ifp->if_index - 1];
528 sdl = (struct sockaddr_dl *)
530 sdl->sdl_type = IFT_ETHER;
531 sdl->sdl_alen = ifp->if_addrlen;
532 bcopy ( (caddr_t)&cup->cu_config.ac_macaddr,
533 LLADDR(sdl), ifp->if_addrlen );
540 * Get adapter configuration information
542 aip = (struct atminfreq *)data;
543 pip = (struct atm_pif *)arg;
544 cup = (Cmn_unit *)pip;
545 acp = &cup->cu_config;
548 * Make sure there's room in user buffer
550 if (aip->air_buf_len < sizeof(acr)) {
556 * Fill in info to be returned
558 KM_ZERO((caddr_t)&acr, sizeof(acr));
559 (void) snprintf(acr.acp_intf, sizeof(acr.acp_intf),
560 "%s%d", pip->pif_name, pip->pif_unit);
561 KM_COPY((caddr_t)acp, (caddr_t)&acr.acp_cfg,
565 * Copy data to user buffer
567 err = copyout((caddr_t)&acr, aip->air_buf_addr,
573 * Update buffer pointer/count
575 aip->air_buf_addr += sizeof(acr);
576 aip->air_buf_len -= sizeof(acr);
581 * Pass off to device-specific handler
583 cup = (Cmn_unit *)arg;
587 err = (*cup->cu_ioctl)(code, data, arg);
599 * Register a Network Convergence Module
601 * Each ATM network convergence module must register itself here before
602 * it will receive network interface status notifications.
605 * ncp pointer to network convergence definition structure
608 * 0 registration successful
609 * errno registration failed - reason indicated
613 atm_netconv_register(ncp)
620 * See if we need to be initialized
626 * Validate protocol family
628 if (ncp->ncm_family > AF_MAX) {
634 * Ensure no duplicates
636 for (tdp = atm_netconv_head; tdp != NULL; tdp = tdp->ncm_next) {
637 if (tdp->ncm_family == ncp->ncm_family) {
646 LINK2TAIL(ncp, struct atm_ncm, atm_netconv_head, ncm_next);
649 * Add new interface output function
651 atm_ifouttbl[ncp->ncm_family] = ncp->ncm_ifoutput;
659 * De-register an ATM Network Convergence Module
661 * Each ATM network convergence provider must de-register its registered
662 * service(s) before terminating. Specifically, loaded kernel modules
663 * must de-register their services before unloading themselves.
666 * ncp pointer to network convergence definition structure
669 * 0 de-registration successful
670 * errno de-registration failed - reason indicated
674 atm_netconv_deregister(ncp)
677 int found, s = splnet();
680 * Remove module from list
682 UNLINKF(ncp, struct atm_ncm, atm_netconv_head, ncm_next, found);
690 * Remove module's interface output function
692 atm_ifouttbl[ncp->ncm_family] = NULL;
700 * Attach an ATM Network Interface
702 * Before an ATM network interface can be used by the system, the owning
703 * device interface must attach the network interface using this function.
704 * The physical interface for this network interface must have been previously
705 * registered (using atm_interface_register). The network interface will be
706 * added to the kernel's interface list and to the physical interface's list.
707 * The caller is responsible for initializing the control block fields.
710 * nip pointer to atm network interface control block
713 * 0 attach successful
714 * errno attach failed - reason indicated
721 struct atm_pif *pip, *pip2;
732 * Verify physical interface is registered
734 for (pip2 = atm_interface_head; pip2 != NULL; pip2 = pip2->pif_next) {
738 if ((pip == NULL) || (pip2 == NULL)) {
744 * Add to system interface list
749 * Add to physical interface list
751 LINK2TAIL(nip, struct atm_nif, pip->pif_nif, nif_pnext);
754 * Notify network convergence modules of new network i/f
756 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
759 err = (*ncp->ncm_stat)(NCM_ATTACH, nip, 0);
773 * Detach an ATM Network Interface
775 * Before an ATM network interface control block can be freed, all kernel
776 * references to/from this block must be released. This function will delete
777 * all routing references to the interface and free all interface addresses
778 * for the interface. The network interface will then be removed from the
779 * kernel's interface list and from the owning physical interface's list.
780 * The caller is responsible for free'ing the control block.
783 * nip pointer to atm network interface control block
795 struct ifnet *ifp = &nip->nif_if;
797 struct in_ifaddr *ia;
798 struct radix_node_head *rnh;
804 * Notify convergence modules of network i/f demise
806 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
807 (void) (*ncp->ncm_stat)(NCM_DETACH, nip, 0);
811 * Mark interface down
816 * Free all interface routes and addresses
823 /* Delete interface route */
826 /* Remove interface address from queues */
828 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
829 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
831 /* Free interface address */
836 * Delete all remaining routes using this interface
837 * Unfortuneatly the only way to do this is to slog through
838 * the entire routing table looking for routes which point
839 * to this interface...oh well...
841 for (i = 1; i <= AF_MAX; i++) {
842 if ((rnh = rt_tables[i]) == NULL)
844 (void) rnh->rnh_walktree(rnh, atm_netif_rtdel, ifp);
848 * Remove from system interface list (ie. if_detach())
850 TAILQ_REMOVE(&ifnet, ifp, if_link);
853 * Remove from physical interface list
855 UNLINK(nip, struct atm_nif, nip->nif_pif->pif_nif, nif_pnext);
862 * Delete Routes for a Network Interface
864 * Called for each routing entry via the rnh->rnh_walktree() call above
865 * to delete all route entries referencing a detaching network interface.
868 * rn pointer to node in the routing table
869 * arg argument passed to rnh->rnh_walktree() - detaching interface
873 * errno failed - reason indicated
877 atm_netif_rtdel(rn, arg)
878 struct radix_node *rn;
881 struct rtentry *rt = (struct rtentry *)rn;
882 struct ifnet *ifp = arg;
885 if (rt->rt_ifp == ifp) {
888 * Protect (sorta) against walktree recursion problems
891 if ((rt->rt_flags & RTF_UP) == 0)
894 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
895 rt_mask(rt), rt->rt_flags,
896 (struct rtentry **) NULL);
898 log(LOG_WARNING, "atm_netif_rtdel: error %d\n", err);
907 * Set an ATM Network Interface address
909 * This is called from a device interface when processing an SIOCSIFADDR
910 * ioctl request. We just notify all convergence modules of the new address
911 * and hope everyone has non-overlapping interests, since if someone reports
912 * an error we don't go back and tell everyone to undo the change.
915 * nip pointer to atm network interface control block
916 * ifa pointer to new interface address
920 * errno set failed - reason indicated
924 atm_nif_setaddr(nip, ifa)
929 int err = 0, s = splnet();
932 * Notify convergence modules of network i/f change
934 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
935 err = (*ncp->ncm_stat)(NCM_SETADDR, nip, (int)ifa);
946 * ATM Interface Packet Output
948 * All ATM network interfaces must have their ifnet if_output address set to
949 * this function. Since no existing network layer code is to be modified
950 * for ATM support, this function serves as the hook to allow network output
951 * packets to be assigned to their proper outbound VCC. Each network address
952 * family which is to be supported over ATM must be assigned an output
953 * packet processing function via atm_netconv_register().
956 * ifp pointer to ifnet structure
957 * m pointer to packet buffer chain to be output
958 * dst pointer to packet's network destination address
961 * 0 packet queued to interface
962 * errno output failed - reason indicated
966 atm_ifoutput(ifp, m, dst, rt)
969 struct sockaddr *dst;
972 u_short fam = dst->sa_family;
973 int (*func)(struct ifnet *, KBuffer *,
977 * Validate address family
981 return (EAFNOSUPPORT);
985 * Hand packet off for dst-to-VCC mapping
987 func = atm_ifouttbl[fam];
990 return (EAFNOSUPPORT);
992 return ((*func)(ifp, m, dst));
997 * Handle interface ioctl requests.
1000 * ifp pointer to network interface structure
1002 * data arguments to/from ioctl
1008 atm_if_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1010 struct ifreq *ifr = (struct ifreq *)data;
1011 struct atm_nif *nip = (struct atm_nif *)ifp;
1018 KM_COPY ( (caddr_t)&(nip->nif_pif->pif_macaddr),
1019 (caddr_t)ifr->ifr_addr.sa_data,
1020 sizeof(struct mac_addr) );
1024 error = atm_nif_setaddr ( nip, (struct ifaddr *)data);
1025 ifp->if_flags |= IFF_UP | IFF_RUNNING | IFF_BROADCAST;
1029 *(short *)data = ifp->if_flags;
1046 * Parse interface name
1048 * Parses an interface name string into a name and a unit component.
1051 * name pointer to interface name string
1052 * namep address to store interface name
1053 * size size available at namep
1054 * unitp address to store interface unit number
1062 atm_ifparse(name, namep, size, unitp)
1069 int len = 0, unit = 0;
1072 * Separate supplied string into name and unit parts.
1077 if (*cp >= '0' && *cp <= '9')
1084 while (*cp && *cp >= '0' && *cp <= '9')
1085 unit = 10 * unit + *cp++ - '0';
1094 * Locate ATM physical interface via name
1096 * Uses the supplied interface name string to locate a registered
1097 * ATM physical interface.
1100 * name pointer to interface name string
1103 * 0 interface not found
1104 * else pointer to atm physical interface structure
1111 struct atm_pif *pip;
1118 if (atm_ifparse(name, n, sizeof(n), &unit))
1119 return ((struct atm_pif *)0);
1122 * Look for the physical interface
1124 for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1125 if ((pip->pif_unit == unit) && (strcmp(pip->pif_name, n) == 0))
1134 * Locate ATM network interface via name
1136 * Uses the supplied interface name string to locate an ATM network interface.
1139 * name pointer to interface name string
1142 * 0 interface not found
1143 * else pointer to atm network interface structure
1150 struct atm_pif *pip;
1151 struct atm_nif *nip;
1158 if (atm_ifparse(name, n, sizeof(n), &unit))
1159 return ((struct atm_nif *)0);
1162 * Search thru each physical interface
1164 for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1166 * Looking for network interface
1168 for (nip = pip->pif_nif; nip; nip = nip->nif_pnext) {
1169 struct ifnet *ifp = (struct ifnet *)nip;
1170 if ((ifp->if_dunit == unit) &&
1171 (strcmp(ifp->if_dname, n) == 0))