📄 ipsec_mast.c
字号:
/* * We call the attach routine to attach another device. */DEBUG_NO_STATIC intipsec_mast_attach(struct net_device *dev, struct net_device *physdev){ int i; struct ipsecpriv *prv = dev->priv; if(dev == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_REVEC, "klips_debug:ipsec_mast_attach: " "no device..."); return -ENODEV; } if(prv == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_REVEC, "klips_debug:ipsec_mast_attach: " "no private space associated with dev=%s", dev->name ? dev->name : "NULL"); return -ENODATA; } prv->dev = physdev; prv->hard_start_xmit = physdev->hard_start_xmit; prv->get_stats = physdev->get_stats; if (physdev->hard_header) { prv->hard_header = physdev->hard_header; dev->hard_header = ipsec_mast_hard_header; } else dev->hard_header = NULL; if (physdev->rebuild_header) { prv->rebuild_header = physdev->rebuild_header; dev->rebuild_header = ipsec_mast_rebuild_header; } else dev->rebuild_header = NULL; if (physdev->set_mac_address) { prv->set_mac_address = physdev->set_mac_address; dev->set_mac_address = ipsec_mast_set_mac_address; } else dev->set_mac_address = NULL; if (physdev->header_cache_update) { prv->header_cache_update = physdev->header_cache_update; dev->header_cache_update = ipsec_mast_cache_update; } else dev->header_cache_update = NULL; dev->hard_header_len = physdev->hard_header_len;/* prv->neigh_setup = physdev->neigh_setup; */ dev->neigh_setup = ipsec_mast_neigh_setup_dev; dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */ prv->mtu = physdev->mtu;#ifdef PHYSDEV_TYPE dev->type = physdev->type; /* ARPHRD_MAST; */#endif /* PHYSDEV_TYPE */ dev->addr_len = physdev->addr_len; for (i=0; i<dev->addr_len; i++) { dev->dev_addr[i] = physdev->dev_addr[i]; }#ifdef CONFIG_KLIPS_DEBUG if(debug_mast & DB_MAST_INIT) { printk(KERN_INFO "klips_debug:ipsec_mast_attach: " "physical device %s being attached has HW address: %2x", physdev->name, physdev->dev_addr[0]); for (i=1; i < physdev->addr_len; i++) { printk(":%02x", physdev->dev_addr[i]); } printk("\n"); }#endif /* CONFIG_KLIPS_DEBUG */ return 0;}/* * We call the detach routine to detach the ipsec mast from another device. */DEBUG_NO_STATIC intipsec_mast_detach(struct net_device *dev){ int i; struct ipsecpriv *prv = dev->priv; if(dev == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_REVEC, "klips_debug:ipsec_mast_detach: " "no device..."); return -ENODEV; } if(prv == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_REVEC, "klips_debug:ipsec_mast_detach: " "no private space associated with dev=%s", dev->name ? dev->name : "NULL"); return -ENODATA; } KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_detach: " "physical device %s being detached from virtual device %s\n", prv->dev ? prv->dev->name : "NULL", dev->name); prv->dev = NULL; prv->hard_start_xmit = NULL; prv->get_stats = NULL; prv->hard_header = NULL;#ifdef DETACH_AND_DOWN dev->hard_header = NULL;#endif /* DETACH_AND_DOWN */ prv->rebuild_header = NULL;#ifdef DETACH_AND_DOWN dev->rebuild_header = NULL;#endif /* DETACH_AND_DOWN */ prv->set_mac_address = NULL;#ifdef DETACH_AND_DOWN dev->set_mac_address = NULL;#endif /* DETACH_AND_DOWN */ prv->header_cache_update = NULL;#ifdef DETACH_AND_DOWN dev->header_cache_update = NULL;#endif /* DETACH_AND_DOWN */#ifdef DETACH_AND_DOWN dev->neigh_setup = NULL;#endif /* DETACH_AND_DOWN */ dev->hard_header_len = 0;#ifdef DETACH_AND_DOWN dev->mtu = 0;#endif /* DETACH_AND_DOWN */ prv->mtu = 0; for (i=0; i<MAX_ADDR_LEN; i++) { dev->dev_addr[i] = 0; } dev->addr_len = 0;#ifdef PHYSDEV_TYPE dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */#endif /* PHYSDEV_TYPE */ return 0;}/* * We call the clear routine to detach all ipsec masts from other devices. */DEBUG_NO_STATIC intipsec_mast_clear(void){ int i; struct net_device *ipsecdev = NULL, *prvdev; struct ipsecpriv *prv; char name[9]; int ret; KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_clear: .\n"); for(i = 0; i < IPSEC_NUM_IF; i++) { sprintf(name, IPSEC_DEV_FORMAT, i); if((ipsecdev = ipsec_dev_get(name)) != NULL) { if((prv = (struct ipsecpriv *)(ipsecdev->priv))) { prvdev = (struct net_device *)(prv->dev); if(prvdev) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_clear: " "physical device for device %s is %s\n", name, prvdev->name); if((ret = ipsec_mast_detach(ipsecdev))) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_clear: " "error %d detatching device %s from device %s.\n", ret, name, prvdev->name); return ret; } } } } } return 0;}DEBUG_NO_STATIC intipsec_mast_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd){ struct ipsecmastconf *cf = (struct ipsecmastconf *)&ifr->ifr_data; struct ipsecpriv *prv = dev->priv; struct net_device *them; /* physical device */#ifdef CONFIG_IP_ALIAS char *colon; char realphysname[IFNAMSIZ];#endif /* CONFIG_IP_ALIAS */ if(dev == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "device not supplied.\n"); return -ENODEV; } KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "tncfg service call #%d for dev=%s\n", cmd, dev->name ? dev->name : "NULL"); switch (cmd) { /* attach a virtual ipsec? device to a physical device */ case IPSEC_SET_DEV: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "calling ipsec_mast_attatch...\n");#ifdef CONFIG_IP_ALIAS /* If this is an IP alias interface, get its real physical name */ strncpy(realphysname, cf->cf_name, IFNAMSIZ); realphysname[IFNAMSIZ-1] = 0; colon = strchr(realphysname, ':'); if (colon) *colon = 0; them = ipsec_dev_get(realphysname);#else /* CONFIG_IP_ALIAS */ them = ipsec_dev_get(cf->cf_name);#endif /* CONFIG_IP_ALIAS */ if (them == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "physical device %s requested is null\n", cf->cf_name); return -ENXIO; } #if 0 if (them->flags & IFF_UP) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "physical device %s requested is not up.\n", cf->cf_name); return -ENXIO; }#endif if (prv && prv->dev) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "virtual device is already connected to %s.\n", prv->dev->name ? prv->dev->name : "NULL"); return -EBUSY; } return ipsec_mast_attach(dev, them); case IPSEC_DEL_DEV: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "calling ipsec_mast_detatch.\n"); if (! prv->dev) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "physical device not connected.\n"); return -ENODEV; } return ipsec_mast_detach(dev); case IPSEC_CLR_DEV: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "calling ipsec_mast_clear.\n"); return ipsec_mast_clear(); default: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_ioctl: " "unknown command %d.\n", cmd); return -EOPNOTSUPP; }}intipsec_mast_device_event(struct notifier_block *unused, unsigned long event, void *ptr){ struct net_device *dev = ptr; struct net_device *ipsec_dev; struct ipsecpriv *priv; char name[9]; int i; if (dev == NULL) { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "dev=NULL for event type %ld.\n", event); return(NOTIFY_DONE); } /* check for loopback devices */ if (dev && (dev->flags & IFF_LOOPBACK)) { return(NOTIFY_DONE); } switch (event) { case NETDEV_DOWN: /* look very carefully at the scope of these compiler directives before changing anything... -- RGB */ case NETDEV_UNREGISTER: switch (event) { case NETDEV_DOWN: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_DOWN dev=%s flags=%x\n", dev->name, dev->flags); if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) { printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n", dev->name); } break; case NETDEV_UNREGISTER: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_UNREGISTER dev=%s flags=%x\n", dev->name, dev->flags); break; } /* find the attached physical device and detach it. */ for(i = 0; i < IPSEC_NUM_IF; i++) { sprintf(name, IPSEC_DEV_FORMAT, i); ipsec_dev = ipsec_dev_get(name); if(ipsec_dev) { priv = (struct ipsecpriv *)(ipsec_dev->priv); if(priv) { ; if(((struct net_device *)(priv->dev)) == dev) { /* dev_close(ipsec_dev); */ /* return */ ipsec_mast_detach(ipsec_dev); KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "device '%s' has been detached.\n", ipsec_dev->name); break; } } else { KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "device '%s' has no private data space!\n", ipsec_dev->name); } } } break; case NETDEV_UP: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_UP dev=%s\n", dev->name); break; case NETDEV_REBOOT: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_REBOOT dev=%s\n", dev->name); break; case NETDEV_CHANGE: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_CHANGE dev=%s flags=%x\n", dev->name, dev->flags); break; case NETDEV_REGISTER: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_REGISTER dev=%s\n", dev->name); break; case NETDEV_CHANGEMTU: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_CHANGEMTU dev=%s to mtu=%d\n", dev->name, dev->mtu); break; case NETDEV_CHANGEADDR: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_CHANGEADDR dev=%s\n", dev->name); break; case NETDEV_GOING_DOWN: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_GOING_DOWN dev=%s\n", dev->name); break; case NETDEV_CHANGENAME: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "NETDEV_CHANGENAME dev=%s\n", dev->name); break; default: KLIPS_PRINT(debug_mast & DB_MAST_INIT, "klips_debug:ipsec_mast_device_event: " "event type %ld unrecognised for dev=%s\n", event, dev->name); break; } return NOTIFY_DONE;}/* * Called when an ipsec mast device is initialized. * The ipsec mast device structure is passed to us. */ intipsec_mast_init(struct net_device *dev){ int i; KLIPS_PRINT(debug_mast, "klips_debug:ipsec_mast_init: " "allocating %lu bytes initialising device: %s\n", (unsigned long) sizeof(struct ipsecpriv), dev->name ? dev->name : "NULL"); /* Add our mast functions to the device */ dev->open = ipsec_mast_open; dev->stop = ipsec_mast_close; dev->hard_start_xmit = ipsec_mast_start_xmit; dev->get_stats = ipsec_mast_get_stats; dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL); if (dev->priv == NULL) return -ENOMEM; memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv)); for(i = 0; i < sizeof(zeroes); i++) { ((__u8*)(zeroes))[i] = 0; } dev->set_multicast_list = NULL; dev->do_ioctl = ipsec_mast_ioctl; dev->hard_header = NULL; dev->rebuild_header = NULL; dev->set_mac_address = NULL; dev->header_cache_update= NULL; dev->neigh_setup = ipsec_mast_neigh_setup_dev; dev->hard_header_len = 0; dev->mtu = 0; dev->addr_len = 0; dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */ /* ARPHRD_ETHER; */ dev->tx_queue_len = 10; /* Small queue */ memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */ /* New-style flags. */ dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */; dev_init_buffers(dev); /* We're done. Have I forgotten anything? */ return 0;}/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* Module specific interface (but it links with the rest of IPSEC) *//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */intipsec_mast_probe(struct net_device *dev){ ipsec_mast_init(dev); return 0;}int ipsec_mast_init_devices(void){ return 0;}/* void */intipsec_mast_cleanup_devices(void){ int error = 0; int i; char name[10]; struct net_device *dev_mast; for(i = 0; i < ipsec_mastdevice_count; i++) { sprintf(name, MAST_DEV_FORMAT, i); if((dev_mast = ipsec_dev_get(name)) == NULL) { break; } unregister_netdev(dev_mast); kfree(dev_mast->priv); dev_mast->priv=NULL; } return error;}/* * $Log: ipsec_mast.c,v $ * Revision 1.6 2004/12/03 21:25:57 mcr * compile time fixes for running on 2.6. * still experimental. * * Revision 1.5 2004/08/03 18:19:08 mcr * in 2.6, use "net_device" instead of #define device->net_device. * this probably breaks 2.0 compiles. * * Revision 1.4 2004/07/10 19:11:18 mcr * CONFIG_IPSEC -> CONFIG_KLIPS. * * Revision 1.3 2003/10/31 02:27:55 mcr * pulled up port-selector patches and sa_id elimination. * * Revision 1.2.4.1 2003/10/29 01:30:41 mcr * elimited "struct sa_id". * * Revision 1.2 2003/06/22 20:06:17 mcr * refactored mast code still had lots of ipsecX junk in it. * * Revision 1.1 2003/02/12 19:31:12 rgb * Refactored from ipsec_tunnel.c * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -