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 $
34 * ATM interface management
38 #include <netatm/kern_include.h>
41 __RCSID("@(#) $FreeBSD: src/sys/netatm/atm_if.c,v 1.5 1999/08/28 00:48:35 peter Exp $");
45 #if (defined(BSD) && (BSD < 199506))
52 static int atm_physif_ioctl __P((int, caddr_t, caddr_t));
53 #if (defined(BSD) && (BSD >= 199306))
54 static int atm_netif_rtdel __P((struct radix_node *, void *));
56 static int atm_if_ioctl __P((struct ifnet *, u_long, caddr_t));
57 static int atm_ifparse __P((char *, char *, int, int *));
62 static int (*atm_ifouttbl[AF_MAX+1])
63 __P((struct ifnet *, KBuffer *, struct sockaddr *))
68 * Register an ATM physical interface
70 * Each ATM device interface must register itself here upon completing
71 * its internal initialization. This applies to both linked and loaded
72 * device drivers. The interface must be registered before a signalling
73 * manager can be attached.
76 * cup pointer to interface's common unit structure
77 * name pointer to device name string
78 * sdp pointer to interface's stack services
81 * 0 registration successful
82 * errno registration failed - reason indicated
86 atm_physif_register(cup, name, sdp)
89 struct stack_defn *sdp;
95 * See if we need to be initialized
101 * Make sure we're not already registered
103 if (cup->cu_flags & CUF_REGISTER) {
110 * Make sure an interface is only registered once
112 for (pip = atm_interface_head; pip != NULL; pip = pip->pif_next) {
113 if ((cup->cu_unit == pip->pif_unit) &&
114 (strcmp(name, pip->pif_name) == 0)) {
121 * Fill in physical interface parameters
124 pip->pif_name = name;
125 pip->pif_unit = cup->cu_unit;
126 pip->pif_flags = PIF_UP;
127 pip->pif_services = sdp;
128 pip->pif_ioctl = atm_physif_ioctl;
131 * Link in the interface and mark us registered
133 LINK2TAIL(pip, struct atm_pif, atm_interface_head, pif_next);
134 cup->cu_flags |= CUF_REGISTER;
142 * De-register an ATM physical interface
144 * Each ATM interface must de-register itself before downing the interface.
145 * The interface's signalling manager will be detached and any network
146 * interface and VCC control blocks will be freed.
149 * cup pointer to interface's common unit structure
152 * 0 de-registration successful
153 * errno de-registration failed - reason indicated
157 atm_physif_deregister(cup)
160 struct atm_pif *pip = (struct atm_pif *)&cup->cu_pif;
166 * Detach and deregister, if needed
168 if ((cup->cu_flags & CUF_REGISTER)) {
171 * Detach from signalling manager
173 if (pip->pif_sigmgr != NULL) {
174 err = atm_sigmgr_detach(pip);
175 if (err && (err != ENOENT)) {
182 * Make sure signalling manager is detached
184 if (pip->pif_sigmgr != NULL) {
192 UNLINK(pip, struct atm_pif, atm_interface_head, pif_next);
194 cup->cu_flags &= ~CUF_REGISTER;
198 * Free all of our network interfaces
200 atm_physif_freenifs(pip);
203 * Free unit's vcc information
210 cup->cu_vcc = (Cmn_vcc *)NULL;
219 * Free all network interfaces on a physical interface
222 * pip pointer to physical interface structure
229 atm_physif_freenifs(pip)
232 struct atm_nif *nip = pip->pif_nif;
238 * atm_nif_detach zeros pointers - save so we can
241 struct atm_nif *nipp = nip->nif_pnext;
244 * Clean up network i/f trails
246 atm_nif_detach ( nip );
247 atm_free ((caddr_t)nip);
250 pip->pif_nif = (struct atm_nif *)NULL;
259 * Handle physical interface ioctl's
261 * See <netatm/atm_ioctl.h> for definitions.
266 * code Ioctl function (sub)code
267 * data Data block. On input contains command,
268 * on output, contains results
269 * arg Optional code specific arguments
272 * 0 Request processed successfully
273 * errno Request failed - reason code
277 atm_physif_ioctl(code, data, arg)
282 struct atminfreq *aip = (struct atminfreq *)data;
283 struct atmsetreq *asr = (struct atmsetreq *)data;
291 caddr_t buf = aip->air_buf_addr;
292 struct air_phy_stat_rsp *apsp;
293 struct air_int_rsp apr;
294 struct air_netif_rsp anr;
295 struct air_cfg_rsp acr;
296 int count, len, buf_len = aip->air_buf_len;
298 char ifname[2*IFNAMSIZ];
299 #if (defined(BSD) && (BSD >= 199103))
301 struct in_ifaddr *ia;
302 struct sockaddr_dl *sdl;
306 switch ( aip->air_opcode ) {
310 * Get physical interface information
312 aip = (struct atminfreq *)data;
313 pip = (struct atm_pif *)arg;
316 * Make sure there's room in user buffer
318 if (aip->air_buf_len < sizeof(apr)) {
324 * Fill in info to be returned
326 KM_ZERO((caddr_t)&apr, sizeof(apr));
327 smp = pip->pif_sigmgr;
328 sip = pip->pif_siginst;
329 (void) snprintf(apr.anp_intf, sizeof(apr.anp_intf),
330 "%s%d", pip->pif_name, pip->pif_unit );
333 strcpy(apr.anp_nif_pref, pip->pif_nif->nif_if.if_name);
338 nip = nip->nif_pnext;
342 ATM_ADDR_COPY(&sip->si_addr, &apr.anp_addr);
343 ATM_ADDR_COPY(&sip->si_subaddr, &apr.anp_subaddr);
344 apr.anp_sig_proto = smp->sm_proto;
345 apr.anp_sig_state = sip->si_state;
349 * Copy data to user buffer
351 err = copyout((caddr_t)&apr, aip->air_buf_addr, sizeof(apr));
356 * Update buffer pointer/count
358 aip->air_buf_addr += sizeof(apr);
359 aip->air_buf_len -= sizeof(apr);
364 * Get network interface information
366 aip = (struct atminfreq *)data;
367 nip = (struct atm_nif *)arg;
372 * Make sure there's room in user buffer
374 if (aip->air_buf_len < sizeof(anr)) {
380 * Fill in info to be returned
382 KM_ZERO((caddr_t)&anr, sizeof(anr));
383 (void) snprintf(anr.anp_intf, sizeof(anr.anp_intf),
384 "%s%d", ifp->if_name, ifp->if_unit);
387 anr.anp_proto_addr = *ia->ia_ifa.ifa_addr;
389 (void) snprintf(anr.anp_phy_intf, sizeof(anr.anp_phy_intf),
390 "%s%d", pip->pif_name, pip->pif_unit);
393 * Copy data to user buffer
395 err = copyout((caddr_t)&anr, aip->air_buf_addr, sizeof(anr));
400 * Update buffer pointer/count
402 aip->air_buf_addr += sizeof(anr);
403 aip->air_buf_len -= sizeof(anr);
408 * Get per interface statistics
410 pip = (struct atm_pif *)arg;
413 snprintf ( ifname, sizeof(ifname),
414 "%s%d", pip->pif_name, pip->pif_unit );
417 * Cast response into users buffer
419 apsp = (struct air_phy_stat_rsp *)buf;
424 len = sizeof ( struct air_phy_stat_rsp );
429 * Copy interface name into response
431 if ((err = copyout ( ifname, apsp->app_intf, IFNAMSIZ)) != 0)
437 if ((err = copyout(&pip->pif_ipdus, &apsp->app_ipdus,
438 len - sizeof(apsp->app_intf))) != 0)
442 * Adjust buffer elements
447 aip->air_buf_addr = buf;
448 aip->air_buf_len = buf_len;
453 * Set NIF - allow user to configure 1 or more logical
454 * interfaces per physical interface.
458 * Get pointer to physical interface structure from
461 pip = (struct atm_pif *)arg;
462 cup = (Cmn_unit *)pip;
465 * Sanity check - are we already connected to something?
467 if ( pip->pif_sigmgr )
474 * Free any previously allocated NIFs
476 atm_physif_freenifs(pip);
479 * Add list of interfaces
481 for ( count = 0; count < asr->asr_nif_cnt; count++ )
483 nip = (struct atm_nif *)atm_allocate(cup->cu_nif_pool);
487 * Destroy any successful nifs
489 atm_physif_freenifs(pip);
497 strcpy ( nip->nif_name, asr->asr_nif_pref );
498 nip->nif_sel = count;
500 ifp->if_name = nip->nif_name;
501 ifp->if_unit = count;
502 ifp->if_mtu = ATM_NIF_MTU;
503 ifp->if_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
504 ifp->if_output = atm_ifoutput;
505 ifp->if_ioctl = atm_if_ioctl;
506 ifp->if_snd.ifq_maxlen = ifqmaxlen;
507 #if (defined(BSD) && (BSD >= 199103))
509 * Set if_type and if_baudrate
511 ifp->if_type = IFT_ATM;
512 switch ( cup->cu_config.ac_media ) {
514 ifp->if_baudrate = 100000000;
517 ifp->if_baudrate = 140000000;
522 ifp->if_baudrate = 155000000;
527 if ((err = atm_nif_attach(nip)) != 0) {
528 atm_free ( (caddr_t)nip );
531 * Destroy any successful nifs
533 atm_physif_freenifs(pip);
536 #if (defined(BSD) && (BSD >= 199103))
538 * Set macaddr in <Link> address
541 ifa = ifnet_addrs[ifp->if_index - 1];
543 sdl = (struct sockaddr_dl *)
545 sdl->sdl_type = IFT_ETHER;
546 sdl->sdl_alen = ifp->if_addrlen;
547 bcopy ( (caddr_t)&cup->cu_config.ac_macaddr,
548 LLADDR(sdl), ifp->if_addrlen );
556 * Get adapter configuration information
558 aip = (struct atminfreq *)data;
559 pip = (struct atm_pif *)arg;
560 cup = (Cmn_unit *)pip;
561 acp = &cup->cu_config;
564 * Make sure there's room in user buffer
566 if (aip->air_buf_len < sizeof(acr)) {
572 * Fill in info to be returned
574 KM_ZERO((caddr_t)&acr, sizeof(acr));
575 (void) snprintf(acr.acp_intf, sizeof(acr.acp_intf),
576 "%s%d", pip->pif_name, pip->pif_unit);
577 KM_COPY((caddr_t)acp, (caddr_t)&acr.acp_cfg,
581 * Copy data to user buffer
583 err = copyout((caddr_t)&acr, aip->air_buf_addr,
589 * Update buffer pointer/count
591 aip->air_buf_addr += sizeof(acr);
592 aip->air_buf_len -= sizeof(acr);
597 * Pass off to device-specific handler
599 cup = (Cmn_unit *)arg;
603 err = (*cup->cu_ioctl)(code, data, arg);
615 * Register a Network Convergence Module
617 * Each ATM network convergence module must register itself here before
618 * it will receive network interface status notifications.
621 * ncp pointer to network convergence definition structure
624 * 0 registration successful
625 * errno registration failed - reason indicated
629 atm_netconv_register(ncp)
636 * See if we need to be initialized
642 * Validate protocol family
644 if (ncp->ncm_family > AF_MAX) {
650 * Ensure no duplicates
652 for (tdp = atm_netconv_head; tdp != NULL; tdp = tdp->ncm_next) {
653 if (tdp->ncm_family == ncp->ncm_family) {
662 LINK2TAIL(ncp, struct atm_ncm, atm_netconv_head, ncm_next);
665 * Add new interface output function
667 atm_ifouttbl[ncp->ncm_family] = ncp->ncm_ifoutput;
675 * De-register an ATM Network Convergence Module
677 * Each ATM network convergence provider must de-register its registered
678 * service(s) before terminating. Specifically, loaded kernel modules
679 * must de-register their services before unloading themselves.
682 * ncp pointer to network convergence definition structure
685 * 0 de-registration successful
686 * errno de-registration failed - reason indicated
690 atm_netconv_deregister(ncp)
693 int found, s = splnet();
696 * Remove module from list
698 UNLINKF(ncp, struct atm_ncm, atm_netconv_head, ncm_next, found);
706 * Remove module's interface output function
708 atm_ifouttbl[ncp->ncm_family] = NULL;
716 * Attach an ATM Network Interface
718 * Before an ATM network interface can be used by the system, the owning
719 * device interface must attach the network interface using this function.
720 * The physical interface for this network interface must have been previously
721 * registered (using atm_interface_register). The network interface will be
722 * added to the kernel's interface list and to the physical interface's list.
723 * The caller is responsible for initializing the control block fields.
726 * nip pointer to atm network interface control block
729 * 0 attach successful
730 * errno attach failed - reason indicated
737 struct atm_pif *pip, *pip2;
748 * Verify physical interface is registered
750 for (pip2 = atm_interface_head; pip2 != NULL; pip2 = pip2->pif_next) {
754 if ((pip == NULL) || (pip2 == NULL)) {
760 * Add to system interface list
765 * Add to physical interface list
767 LINK2TAIL(nip, struct atm_nif, pip->pif_nif, nif_pnext);
770 * Notify network convergence modules of new network i/f
772 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
775 err = (*ncp->ncm_stat)(NCM_ATTACH, nip, 0);
789 * Detach an ATM Network Interface
791 * Before an ATM network interface control block can be freed, all kernel
792 * references to/from this block must be released. This function will delete
793 * all routing references to the interface and free all interface addresses
794 * for the interface. The network interface will then be removed from the
795 * kernel's interface list and from the owning physical interface's list.
796 * The caller is responsible for free'ing the control block.
799 * nip pointer to atm network interface control block
811 struct ifnet *ifp = &nip->nif_if;
813 struct in_ifaddr *ia;
814 struct radix_node_head *rnh;
820 * Notify convergence modules of network i/f demise
822 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
823 (void) (*ncp->ncm_stat)(NCM_DETACH, nip, 0);
827 * Mark interface down
832 * Free all interface routes and addresses
839 /* Delete interface route */
842 /* Remove interface address from queues */
844 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
845 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
847 /* Free interface address */
852 * Delete all remaining routes using this interface
853 * Unfortuneatly the only way to do this is to slog through
854 * the entire routing table looking for routes which point
855 * to this interface...oh well...
857 for (i = 1; i <= AF_MAX; i++) {
858 if ((rnh = rt_tables[i]) == NULL)
860 (void) rnh->rnh_walktree(rnh, atm_netif_rtdel, ifp);
864 * Remove from system interface list (ie. if_detach())
866 TAILQ_REMOVE(&ifnet, ifp, if_link);
869 * Remove from physical interface list
871 UNLINK(nip, struct atm_nif, nip->nif_pif->pif_nif, nif_pnext);
878 * Delete Routes for a Network Interface
880 * Called for each routing entry via the rnh->rnh_walktree() call above
881 * to delete all route entries referencing a detaching network interface.
884 * rn pointer to node in the routing table
885 * arg argument passed to rnh->rnh_walktree() - detaching interface
889 * errno failed - reason indicated
893 atm_netif_rtdel(rn, arg)
894 struct radix_node *rn;
897 struct rtentry *rt = (struct rtentry *)rn;
898 struct ifnet *ifp = arg;
901 if (rt->rt_ifp == ifp) {
904 * Protect (sorta) against walktree recursion problems
907 if ((rt->rt_flags & RTF_UP) == 0)
910 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
911 rt_mask(rt), rt->rt_flags,
912 (struct rtentry **) NULL);
914 log(LOG_WARNING, "atm_netif_rtdel: error %d\n", err);
923 * Set an ATM Network Interface address
925 * This is called from a device interface when processing an SIOCSIFADDR
926 * ioctl request. We just notify all convergence modules of the new address
927 * and hope everyone has non-overlapping interests, since if someone reports
928 * an error we don't go back and tell everyone to undo the change.
931 * nip pointer to atm network interface control block
932 * ifa pointer to new interface address
936 * errno set failed - reason indicated
940 atm_nif_setaddr(nip, ifa)
945 int err = 0, s = splnet();
948 * Notify convergence modules of network i/f change
950 for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
951 err = (*ncp->ncm_stat)(NCM_SETADDR, nip, (int)ifa);
962 * ATM Interface Packet Output
964 * All ATM network interfaces must have their ifnet if_output address set to
965 * this function. Since no existing network layer code is to be modified
966 * for ATM support, this function serves as the hook to allow network output
967 * packets to be assigned to their proper outbound VCC. Each network address
968 * family which is to be supported over ATM must be assigned an output
969 * packet processing function via atm_netconv_register().
972 * ifp pointer to ifnet structure
973 * m pointer to packet buffer chain to be output
974 * dst pointer to packet's network destination address
977 * 0 packet queued to interface
978 * errno output failed - reason indicated
982 #if (defined(BSD) && (BSD >= 199103))
983 atm_ifoutput(ifp, m, dst, rt)
985 atm_ifoutput(ifp, m, dst)
989 struct sockaddr *dst;
990 #if (defined(BSD) && (BSD >= 199103))
994 u_short fam = dst->sa_family;
995 int (*func)__P((struct ifnet *, KBuffer *,
999 * Validate address family
1003 return (EAFNOSUPPORT);
1007 * Hand packet off for dst-to-VCC mapping
1009 func = atm_ifouttbl[fam];
1012 return (EAFNOSUPPORT);
1014 return ((*func)(ifp, m, dst));
1019 * Handle interface ioctl requests.
1022 * ifp pointer to network interface structure
1024 * data arguments to/from ioctl
1030 atm_if_ioctl(ifp, cmd, data)
1035 register struct ifreq *ifr = (struct ifreq *)data;
1036 struct atm_nif *nip = (struct atm_nif *)ifp;
1043 KM_COPY ( (caddr_t)&(nip->nif_pif->pif_macaddr),
1044 (caddr_t)ifr->ifr_addr.sa_data,
1045 sizeof(struct mac_addr) );
1049 error = atm_nif_setaddr ( nip, (struct ifaddr *)data);
1050 ifp->if_flags |= IFF_UP | IFF_RUNNING | IFF_BROADCAST;
1054 *(short *)data = ifp->if_flags;
1071 * Parse interface name
1073 * Parses an interface name string into a name and a unit component.
1076 * name pointer to interface name string
1077 * namep address to store interface name
1078 * size size available at namep
1079 * unitp address to store interface unit number
1087 atm_ifparse(name, namep, size, unitp)
1094 int len = 0, unit = 0;
1097 * Separate supplied string into name and unit parts.
1102 if (*cp >= '0' && *cp <= '9')
1109 while (*cp && *cp >= '0' && *cp <= '9')
1110 unit = 10 * unit + *cp++ - '0';
1119 * Locate ATM physical interface via name
1121 * Uses the supplied interface name string to locate a registered
1122 * ATM physical interface.
1125 * name pointer to interface name string
1128 * 0 interface not found
1129 * else pointer to atm physical interface structure
1136 struct atm_pif *pip;
1143 if (atm_ifparse(name, n, sizeof(n), &unit))
1144 return ((struct atm_pif *)0);
1147 * Look for the physical interface
1149 for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1150 if ((pip->pif_unit == unit) && (strcmp(pip->pif_name, n) == 0))
1159 * Locate ATM network interface via name
1161 * Uses the supplied interface name string to locate an ATM network interface.
1164 * name pointer to interface name string
1167 * 0 interface not found
1168 * else pointer to atm network interface structure
1175 struct atm_pif *pip;
1176 struct atm_nif *nip;
1183 if (atm_ifparse(name, n, sizeof(n), &unit))
1184 return ((struct atm_nif *)0);
1187 * Search thru each physical interface
1189 for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1191 * Looking for network interface
1193 for (nip = pip->pif_nif; nip; nip = nip->nif_pnext) {
1194 struct ifnet *ifp = (struct ifnet *)nip;
1195 if ((ifp->if_unit == unit) &&
1196 (strcmp(ifp->if_name, n) == 0))