[DUMMY]: Keep dummy devices on list
[safe/jmp/linux-2.6] / drivers / net / dummy.c
1 /* dummy.c: a dummy net driver
2
3         The purpose of this driver is to provide a device to point a
4         route through, but not to actually transmit packets.
5
6         Why?  If you have a machine whose only connection is an occasional
7         PPP/SLIP/PLIP link, you can only connect to your own hostname
8         when the link is up.  Otherwise you have to use localhost.
9         This isn't very consistent.
10
11         One solution is to set up a dummy link using PPP/SLIP/PLIP,
12         but this seems (to me) too much overhead for too little gain.
13         This driver provides a small alternative. Thus you can do
14
15         [when not running slip]
16                 ifconfig dummy slip.addr.ess.here up
17         [to go to slip]
18                 ifconfig dummy down
19                 dip whatever
20
21         This was written by looking at Donald Becker's skeleton driver
22         and the loopback driver.  I then threw away anything that didn't
23         apply!  Thanks to Alan Cox for the key clue on what to do with
24         misguided packets.
25
26                         Nick Holloway, 27th May 1994
27         [I tweaked this explanation a little but that's all]
28                         Alan Cox, 30th May 1994
29 */
30
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/init.h>
36 #include <linux/moduleparam.h>
37 #include <linux/rtnetlink.h>
38
39 struct dummy_priv {
40         struct net_device *dev;
41         struct list_head list;
42 };
43
44 static int numdummies = 1;
45
46 static int dummy_xmit(struct sk_buff *skb, struct net_device *dev);
47
48 static int dummy_set_address(struct net_device *dev, void *p)
49 {
50         struct sockaddr *sa = p;
51
52         if (!is_valid_ether_addr(sa->sa_data))
53                 return -EADDRNOTAVAIL;
54
55         memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
56         return 0;
57 }
58
59 /* fake multicast ability */
60 static void set_multicast_list(struct net_device *dev)
61 {
62 }
63
64 static void __init dummy_setup(struct net_device *dev)
65 {
66         /* Initialize the device structure. */
67         dev->hard_start_xmit = dummy_xmit;
68         dev->set_multicast_list = set_multicast_list;
69         dev->set_mac_address = dummy_set_address;
70
71         /* Fill in device structure with ethernet-generic values. */
72         ether_setup(dev);
73         dev->tx_queue_len = 0;
74         dev->change_mtu = NULL;
75         dev->flags |= IFF_NOARP;
76         dev->flags &= ~IFF_MULTICAST;
77         SET_MODULE_OWNER(dev);
78         random_ether_addr(dev->dev_addr);
79 }
80
81 static int dummy_xmit(struct sk_buff *skb, struct net_device *dev)
82 {
83         dev->stats.tx_packets++;
84         dev->stats.tx_bytes += skb->len;
85
86         dev_kfree_skb(skb);
87         return 0;
88 }
89
90 static LIST_HEAD(dummies);
91
92 /* Number of dummy devices to be set up by this module. */
93 module_param(numdummies, int, 0);
94 MODULE_PARM_DESC(numdummies, "Number of dummy pseudo devices");
95
96 static int __init dummy_init_one(void)
97 {
98         struct net_device *dev_dummy;
99         struct dummy_priv *priv;
100         int err;
101
102         dev_dummy = alloc_netdev(sizeof(struct dummy_priv), "dummy%d",
103                                  dummy_setup);
104
105         if (!dev_dummy)
106                 return -ENOMEM;
107
108         if ((err = register_netdev(dev_dummy))) {
109                 free_netdev(dev_dummy);
110                 dev_dummy = NULL;
111         } else {
112                 priv = netdev_priv(dev_dummy);
113                 priv->dev = dev_dummy;
114                 list_add_tail(&priv->list, &dummies);
115         }
116
117         return err;
118 }
119
120 static void dummy_free_one(struct net_device *dev)
121 {
122         struct dummy_priv *priv = netdev_priv(dev);
123
124         list_del(&priv->list);
125         unregister_netdev(dev);
126         free_netdev(dev);
127 }
128
129 static int __init dummy_init_module(void)
130 {
131         struct dummy_priv *priv, *next;
132         int i, err = 0;
133
134         for (i = 0; i < numdummies && !err; i++)
135                 err = dummy_init_one();
136         if (err) {
137                 list_for_each_entry_safe(priv, next, &dummies, list)
138                         dummy_free_one(priv->dev);
139         }
140         return err;
141 }
142
143 static void __exit dummy_cleanup_module(void)
144 {
145         struct dummy_priv *priv, *next;
146
147         list_for_each_entry_safe(priv, next, &dummies, list)
148                 dummy_free_one(priv->dev);
149 }
150
151 module_init(dummy_init_module);
152 module_exit(dummy_cleanup_module);
153 MODULE_LICENSE("GPL");