00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <linux/config.h>
00015 #include <linux/module.h>
00016
00017 #include <linux/kernel.h>
00018 #include <linux/sched.h>
00019 #include <linux/malloc.h>
00020 #include <linux/string.h>
00021 #include <linux/errno.h>
00022
00023 #include <linux/netdevice.h>
00024 #include <linux/inetdevice.h>
00025 #include <linux/etherdevice.h>
00026 #include <linux/skbuff.h>
00027 #include <linux/init.h>
00028
00029 #include <net/sock.h>
00030 #include <linux/netlink.h>
00031
00032
00033
00034
00035
00036 int ethertap_probe(struct device *dev);
00037 static int ethertap_open(struct device *dev);
00038 static int ethertap_start_xmit(struct sk_buff *skb, struct device *dev);
00039 static int ethertap_close(struct device *dev);
00040 static struct net_device_stats *ethertap_get_stats(struct device *dev);
00041 static void ethertap_rx(struct sock *sk, int len);
00042 #ifdef CONFIG_ETHERTAP_MC
00043 static void set_multicast_list(struct device *dev);
00044 #endif
00045
00046 static int ethertap_debug = 0;
00047
00048 static struct device *tap_map[32];
00049
00050
00051
00052
00053
00054 struct net_local
00055 {
00056 struct sock *nl;
00057 #ifdef CONFIG_ETHERTAP_MC
00058 __u32 groups;
00059 #endif
00060 struct net_device_stats stats;
00061 };
00062
00063
00064
00065
00066
00067
00068 __initfunc(int ethertap_probe(struct device *dev))
00069 {
00070 memcpy(dev->dev_addr, "\xFE\xFD\x00\x00\x00\x00", 6);
00071 if (dev->mem_start & 0xf)
00072 ethertap_debug = dev->mem_start & 0x7;
00073
00074
00075
00076
00077
00078 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
00079 if (dev->priv == NULL)
00080 return -ENOMEM;
00081 memset(dev->priv, 0, sizeof(struct net_local));
00082
00083
00084
00085
00086
00087 dev->open = ethertap_open;
00088 dev->hard_start_xmit = ethertap_start_xmit;
00089 dev->stop = ethertap_close;
00090 dev->get_stats = ethertap_get_stats;
00091 #ifdef CONFIG_ETHERTAP_MC
00092 dev->set_multicast_list = set_multicast_list;
00093 #endif
00094
00095
00096
00097
00098
00099 ether_setup(dev);
00100
00101 dev->tx_queue_len = 0;
00102 dev->flags|=IFF_NOARP;
00103 tap_map[dev->base_addr]=dev;
00104
00105 return 0;
00106 }
00107
00108
00109
00110
00111
00112 static int ethertap_open(struct device *dev)
00113 {
00114 struct net_local *lp = (struct net_local*)dev->priv;
00115
00116 if (ethertap_debug > 2)
00117 printk("%s: Doing ethertap_open()...", dev->name);
00118
00119 MOD_INC_USE_COUNT;
00120
00121 lp->nl = netlink_kernel_create(dev->base_addr, ethertap_rx);
00122 if (lp->nl == NULL) {
00123 MOD_DEC_USE_COUNT;
00124 return -ENOBUFS;
00125 }
00126
00127 dev->start = 1;
00128 dev->tbusy = 0;
00129 return 0;
00130 }
00131
00132 #ifdef CONFIG_ETHERTAP_MC
00133 static unsigned ethertap_mc_hash(__u8 *dest)
00134 {
00135 unsigned idx = 0;
00136 idx ^= dest[0];
00137 idx ^= dest[1];
00138 idx ^= dest[2];
00139 idx ^= dest[3];
00140 idx ^= dest[4];
00141 idx ^= dest[5];
00142 return 1U << (idx&0x1F);
00143 }
00144
00145 static void set_multicast_list(struct device *dev)
00146 {
00147 unsigned groups = ~0;
00148 struct net_local *lp = (struct net_local *)dev->priv;
00149
00150 if (!(dev->flags&(IFF_NOARP|IFF_PROMISC|IFF_ALLMULTI))) {
00151 struct dev_mc_list *dmi;
00152
00153 groups = ethertap_mc_hash(dev->broadcast);
00154
00155 for (dmi=dev->mc_list; dmi; dmi=dmi->next) {
00156 if (dmi->dmi_addrlen != 6)
00157 continue;
00158 groups |= ethertap_mc_hash(dmi->dmi_addr);
00159 }
00160 }
00161 lp->groups = groups;
00162 if (lp->nl)
00163 lp->nl->protinfo.af_netlink.groups = groups;
00164 }
00165 #endif
00166
00167
00168
00169
00170
00171
00172 static int ethertap_start_xmit(struct sk_buff *skb, struct device *dev)
00173 {
00174 struct net_local *lp = (struct net_local *)dev->priv;
00175 #ifdef CONFIG_ETHERTAP_MC
00176 struct ethhdr *eth = (struct ethhdr*)skb->data;
00177 #endif
00178
00179 if (skb_headroom(skb) < 2) {
00180 static int once;
00181 struct sk_buff *skb2;
00182
00183 if (!once) {
00184 once = 1;
00185 printk(KERN_DEBUG "%s: not aligned xmit by protocol %04x\n", dev->name, skb->protocol);
00186 }
00187
00188 skb2 = skb_realloc_headroom(skb, 2);
00189 dev_kfree_skb(skb);
00190 if (skb2 == NULL)
00191 return 0;
00192 skb = skb2;
00193 }
00194 __skb_push(skb, 2);
00195
00196
00197 if (skb_shared(skb)) {
00198 struct sk_buff *skb2 = skb;
00199 skb = skb_clone(skb, GFP_ATOMIC);
00200 if (skb==NULL) {
00201 dev_kfree_skb(skb2);
00202 return 0;
00203 }
00204 dev_kfree_skb(skb2);
00205 }
00206
00207
00208 lp->stats.tx_bytes+=skb->len;
00209 lp->stats.tx_packets++;
00210
00211 #ifndef CONFIG_ETHERTAP_MC
00212 netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
00213 #else
00214 if (dev->flags&IFF_NOARP) {
00215 netlink_broadcast(lp->nl, skb, 0, ~0, GFP_ATOMIC);
00216 return 0;
00217 }
00218
00219 if (!(eth->h_dest[0]&1)) {
00220
00221 __u32 pid;
00222 memcpy(&pid, eth->h_dest+2, 4);
00223 netlink_unicast(lp->nl, skb, ntohl(pid), MSG_DONTWAIT);
00224 } else
00225 netlink_broadcast(lp->nl, skb, 0, ethertap_mc_hash(eth->h_dest), GFP_ATOMIC);
00226 #endif
00227 return 0;
00228 }
00229
00230 static __inline__ int ethertap_rx_skb(struct sk_buff *skb, struct device *dev)
00231 {
00232 struct net_local *lp = (struct net_local *)dev->priv;
00233 #ifdef CONFIG_ETHERTAP_MC
00234 struct ethhdr *eth = (struct ethhdr*)(skb->data + 2);
00235 #endif
00236 int len = skb->len;
00237
00238 if (len < 16) {
00239 printk(KERN_DEBUG "%s : rx len = %d\n", dev->name, len);
00240 kfree_skb(skb);
00241 return -EINVAL;
00242 }
00243 if (NETLINK_CREDS(skb)->uid) {
00244 printk(KERN_INFO "%s : user %d\n", dev->name, NETLINK_CREDS(skb)->uid);
00245 kfree_skb(skb);
00246 return -EPERM;
00247 }
00248
00249 #ifdef CONFIG_ETHERTAP_MC
00250 if (!(dev->flags&(IFF_NOARP|IFF_PROMISC))) {
00251 int drop = 0;
00252
00253 if (eth->h_dest[0]&1) {
00254 if (!(ethertap_mc_hash(eth->h_dest)&lp->groups))
00255 drop = 1;
00256 } else if (memcmp(eth->h_dest, dev->dev_addr, 6) != 0)
00257 drop = 1;
00258
00259 if (drop) {
00260 if (ethertap_debug > 3)
00261 printk(KERN_DEBUG "%s : not for us\n", dev->name);
00262 kfree_skb(skb);
00263 return -EINVAL;
00264 }
00265 }
00266 #endif
00267
00268 if (skb_shared(skb)) {
00269 struct sk_buff *skb2 = skb;
00270 skb = skb_clone(skb, GFP_KERNEL);
00271 if (skb==NULL) {
00272 kfree_skb(skb2);
00273 return -ENOBUFS;
00274 }
00275 kfree_skb(skb2);
00276 } else
00277 skb_orphan(skb);
00278
00279 skb_pull(skb, 2);
00280 skb->dev = dev;
00281 skb->protocol=eth_type_trans(skb,dev);
00282 memset(skb->cb, 0, sizeof(skb->cb));
00283 lp->stats.rx_packets++;
00284 lp->stats.rx_bytes+=len;
00285 netif_rx(skb);
00286 return len;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296 static void ethertap_rx(struct sock *sk, int len)
00297 {
00298 struct device *dev = tap_map[sk->protocol];
00299 struct sk_buff *skb;
00300
00301 if (dev==NULL) {
00302 printk(KERN_CRIT "ethertap: bad unit!\n");
00303 skb_queue_purge(&sk->receive_queue);
00304 return;
00305 }
00306
00307 if (ethertap_debug > 3)
00308 printk("%s: ethertap_rx()\n", dev->name);
00309
00310 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL)
00311 ethertap_rx_skb(skb, dev);
00312 }
00313
00314 static int ethertap_close(struct device *dev)
00315 {
00316 struct net_local *lp = (struct net_local *)dev->priv;
00317 struct sock *sk = lp->nl;
00318
00319 if (ethertap_debug > 2)
00320 printk("%s: Shutting down.\n", dev->name);
00321
00322 dev->tbusy = 1;
00323 dev->start = 0;
00324
00325 if (sk) {
00326 lp->nl = NULL;
00327 sock_release(sk->socket);
00328 }
00329
00330 MOD_DEC_USE_COUNT;
00331 return 0;
00332 }
00333
00334 static struct net_device_stats *ethertap_get_stats(struct device *dev)
00335 {
00336 struct net_local *lp = (struct net_local *)dev->priv;
00337 return &lp->stats;
00338 }
00339
00340 #ifdef MODULE
00341
00342 static int unit;
00343 MODULE_PARM(unit,"i");
00344
00345 static char devicename[9] = { 0, };
00346
00347 static struct device dev_ethertap =
00348 {
00349 devicename,
00350 0, 0, 0, 0,
00351 1, 5,
00352 0, 0, 0, NULL, ethertap_probe
00353 };
00354
00355 int init_module(void)
00356 {
00357 dev_ethertap.base_addr=unit+NETLINK_TAPBASE;
00358 sprintf(devicename,"tap%d",unit);
00359 if (dev_get(devicename))
00360 {
00361 printk(KERN_INFO "%s already loaded.\n", devicename);
00362 return -EBUSY;
00363 }
00364 if (register_netdev(&dev_ethertap) != 0)
00365 return -EIO;
00366 return 0;
00367 }
00368
00369 void cleanup_module(void)
00370 {
00371 tap_map[dev_ethertap.base_addr]=NULL;
00372 unregister_netdev(&dev_ethertap);
00373
00374
00375
00376
00377
00378 kfree(dev_ethertap.priv);
00379 dev_ethertap.priv = NULL;
00380 }
00381
00382 #endif