Loading...
1/*
2 * DLCI Implementation of Frame Relay protocol for Linux, according to
3 * RFC 1490. This generic device provides en/decapsulation for an
4 * underlying hardware driver. Routes & IPs are assigned to these
5 * interfaces. Requires 'dlcicfg' program to create usable
6 * interfaces, the initial one, 'dlci' is for IOCTL use only.
7 *
8 * Version: @(#)dlci.c 0.35 4 Jan 1997
9 *
10 * Author: Mike McLagan <mike.mclagan@linux.org>
11 *
12 * Changes:
13 *
14 * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
15 * DLCI_RET handling
16 * 0.20 Mike McLagan More conservative on which packets
17 * are returned for retry and which are
18 * are dropped. If DLCI_RET_DROP is
19 * returned from the FRAD, the packet is
20 * sent back to Linux for re-transmission
21 * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
22 * 0.30 Jim Freeman Fixed to allow IPX traffic
23 * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version
28 * 2 of the License, or (at your option) any later version.
29 */
30
31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/fcntl.h>
37#include <linux/interrupt.h>
38#include <linux/ptrace.h>
39#include <linux/ioport.h>
40#include <linux/in.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/string.h>
44#include <linux/errno.h>
45#include <linux/netdevice.h>
46#include <linux/skbuff.h>
47#include <linux/if_arp.h>
48#include <linux/if_frad.h>
49#include <linux/bitops.h>
50
51#include <net/sock.h>
52
53#include <asm/system.h>
54#include <asm/io.h>
55#include <asm/dma.h>
56#include <asm/uaccess.h>
57
58static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
59
60static LIST_HEAD(dlci_devs);
61
62static void dlci_setup(struct net_device *);
63
64/*
65 * these encapsulate the RFC 1490 requirements as well as
66 * deal with packet transmission and reception, working with
67 * the upper network layers
68 */
69
70static int dlci_header(struct sk_buff *skb, struct net_device *dev,
71 unsigned short type, const void *daddr,
72 const void *saddr, unsigned len)
73{
74 struct frhdr hdr;
75 struct dlci_local *dlp;
76 unsigned int hlen;
77 char *dest;
78
79 dlp = netdev_priv(dev);
80
81 hdr.control = FRAD_I_UI;
82 switch (type)
83 {
84 case ETH_P_IP:
85 hdr.IP_NLPID = FRAD_P_IP;
86 hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
87 break;
88
89 /* feel free to add other types, if necessary */
90
91 default:
92 hdr.pad = FRAD_P_PADDING;
93 hdr.NLPID = FRAD_P_SNAP;
94 memset(hdr.OUI, 0, sizeof(hdr.OUI));
95 hdr.PID = htons(type);
96 hlen = sizeof(hdr);
97 break;
98 }
99
100 dest = skb_push(skb, hlen);
101 if (!dest)
102 return 0;
103
104 memcpy(dest, &hdr, hlen);
105
106 return hlen;
107}
108
109static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
110{
111 struct dlci_local *dlp;
112 struct frhdr *hdr;
113 int process, header;
114
115 dlp = netdev_priv(dev);
116 if (!pskb_may_pull(skb, sizeof(*hdr))) {
117 netdev_notice(dev, "invalid data no header\n");
118 dev->stats.rx_errors++;
119 kfree_skb(skb);
120 return;
121 }
122
123 hdr = (struct frhdr *) skb->data;
124 process = 0;
125 header = 0;
126 skb->dev = dev;
127
128 if (hdr->control != FRAD_I_UI)
129 {
130 netdev_notice(dev, "Invalid header flag 0x%02X\n",
131 hdr->control);
132 dev->stats.rx_errors++;
133 }
134 else
135 switch (hdr->IP_NLPID)
136 {
137 case FRAD_P_PADDING:
138 if (hdr->NLPID != FRAD_P_SNAP)
139 {
140 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
141 hdr->NLPID);
142 dev->stats.rx_errors++;
143 break;
144 }
145
146 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
147 {
148 netdev_notice(dev, "Unsupported organizationally unique identifier 0x%02X-%02X-%02X\n",
149 hdr->OUI[0],
150 hdr->OUI[1],
151 hdr->OUI[2]);
152 dev->stats.rx_errors++;
153 break;
154 }
155
156 /* at this point, it's an EtherType frame */
157 header = sizeof(struct frhdr);
158 /* Already in network order ! */
159 skb->protocol = hdr->PID;
160 process = 1;
161 break;
162
163 case FRAD_P_IP:
164 header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
165 skb->protocol = htons(ETH_P_IP);
166 process = 1;
167 break;
168
169 case FRAD_P_SNAP:
170 case FRAD_P_Q933:
171 case FRAD_P_CLNP:
172 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
173 hdr->pad);
174 dev->stats.rx_errors++;
175 break;
176
177 default:
178 netdev_notice(dev, "Invalid pad byte 0x%02X\n",
179 hdr->pad);
180 dev->stats.rx_errors++;
181 break;
182 }
183
184 if (process)
185 {
186 /* we've set up the protocol, so discard the header */
187 skb_reset_mac_header(skb);
188 skb_pull(skb, header);
189 dev->stats.rx_bytes += skb->len;
190 netif_rx(skb);
191 dev->stats.rx_packets++;
192 }
193 else
194 dev_kfree_skb(skb);
195}
196
197static netdev_tx_t dlci_transmit(struct sk_buff *skb, struct net_device *dev)
198{
199 struct dlci_local *dlp = netdev_priv(dev);
200
201 if (skb)
202 dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave);
203 return NETDEV_TX_OK;
204}
205
206static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
207{
208 struct dlci_conf config;
209 struct dlci_local *dlp;
210 struct frad_local *flp;
211 int err;
212
213 dlp = netdev_priv(dev);
214
215 flp = netdev_priv(dlp->slave);
216
217 if (!get)
218 {
219 if (copy_from_user(&config, conf, sizeof(struct dlci_conf)))
220 return -EFAULT;
221 if (config.flags & ~DLCI_VALID_FLAGS)
222 return -EINVAL;
223 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
224 dlp->configured = 1;
225 }
226
227 err = (*flp->dlci_conf)(dlp->slave, dev, get);
228 if (err)
229 return err;
230
231 if (get)
232 {
233 if (copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
234 return -EFAULT;
235 }
236
237 return 0;
238}
239
240static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
241{
242 struct dlci_local *dlp;
243
244 if (!capable(CAP_NET_ADMIN))
245 return -EPERM;
246
247 dlp = netdev_priv(dev);
248
249 switch (cmd)
250 {
251 case DLCI_GET_SLAVE:
252 if (!*(short *)(dev->dev_addr))
253 return -EINVAL;
254
255 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
256 break;
257
258 case DLCI_GET_CONF:
259 case DLCI_SET_CONF:
260 if (!*(short *)(dev->dev_addr))
261 return -EINVAL;
262
263 return dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF);
264 break;
265
266 default:
267 return -EOPNOTSUPP;
268 }
269 return 0;
270}
271
272static int dlci_change_mtu(struct net_device *dev, int new_mtu)
273{
274 struct dlci_local *dlp = netdev_priv(dev);
275
276 return dev_set_mtu(dlp->slave, new_mtu);
277}
278
279static int dlci_open(struct net_device *dev)
280{
281 struct dlci_local *dlp;
282 struct frad_local *flp;
283 int err;
284
285 dlp = netdev_priv(dev);
286
287 if (!*(short *)(dev->dev_addr))
288 return -EINVAL;
289
290 if (!netif_running(dlp->slave))
291 return -ENOTCONN;
292
293 flp = netdev_priv(dlp->slave);
294 err = (*flp->activate)(dlp->slave, dev);
295 if (err)
296 return err;
297
298 netif_start_queue(dev);
299
300 return 0;
301}
302
303static int dlci_close(struct net_device *dev)
304{
305 struct dlci_local *dlp;
306 struct frad_local *flp;
307 int err;
308
309 netif_stop_queue(dev);
310
311 dlp = netdev_priv(dev);
312
313 flp = netdev_priv(dlp->slave);
314 err = (*flp->deactivate)(dlp->slave, dev);
315
316 return 0;
317}
318
319static int dlci_add(struct dlci_add *dlci)
320{
321 struct net_device *master, *slave;
322 struct dlci_local *dlp;
323 struct frad_local *flp;
324 int err = -EINVAL;
325
326
327 /* validate slave device */
328 slave = dev_get_by_name(&init_net, dlci->devname);
329 if (!slave)
330 return -ENODEV;
331
332 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
333 goto err1;
334
335 /* create device name */
336 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
337 dlci_setup);
338 if (!master) {
339 err = -ENOMEM;
340 goto err1;
341 }
342
343 /* make sure same slave not already registered */
344 rtnl_lock();
345 list_for_each_entry(dlp, &dlci_devs, list) {
346 if (dlp->slave == slave) {
347 err = -EBUSY;
348 goto err2;
349 }
350 }
351
352 *(short *)(master->dev_addr) = dlci->dlci;
353
354 dlp = netdev_priv(master);
355 dlp->slave = slave;
356 dlp->master = master;
357
358 flp = netdev_priv(slave);
359 err = (*flp->assoc)(slave, master);
360 if (err < 0)
361 goto err2;
362
363 err = register_netdevice(master);
364 if (err < 0)
365 goto err2;
366
367 strcpy(dlci->devname, master->name);
368
369 list_add(&dlp->list, &dlci_devs);
370 rtnl_unlock();
371
372 return 0;
373
374 err2:
375 rtnl_unlock();
376 free_netdev(master);
377 err1:
378 dev_put(slave);
379 return err;
380}
381
382static int dlci_del(struct dlci_add *dlci)
383{
384 struct dlci_local *dlp;
385 struct frad_local *flp;
386 struct net_device *master, *slave;
387 int err;
388
389 /* validate slave device */
390 master = __dev_get_by_name(&init_net, dlci->devname);
391 if (!master)
392 return -ENODEV;
393
394 if (netif_running(master)) {
395 return -EBUSY;
396 }
397
398 dlp = netdev_priv(master);
399 slave = dlp->slave;
400 flp = netdev_priv(slave);
401
402 rtnl_lock();
403 err = (*flp->deassoc)(slave, master);
404 if (!err) {
405 list_del(&dlp->list);
406
407 unregister_netdevice(master);
408
409 dev_put(slave);
410 }
411 rtnl_unlock();
412
413 return err;
414}
415
416static int dlci_ioctl(unsigned int cmd, void __user *arg)
417{
418 struct dlci_add add;
419 int err;
420
421 if (!capable(CAP_NET_ADMIN))
422 return -EPERM;
423
424 if (copy_from_user(&add, arg, sizeof(struct dlci_add)))
425 return -EFAULT;
426
427 switch (cmd)
428 {
429 case SIOCADDDLCI:
430 err = dlci_add(&add);
431
432 if (!err)
433 if (copy_to_user(arg, &add, sizeof(struct dlci_add)))
434 return -EFAULT;
435 break;
436
437 case SIOCDELDLCI:
438 err = dlci_del(&add);
439 break;
440
441 default:
442 err = -EINVAL;
443 }
444
445 return err;
446}
447
448static const struct header_ops dlci_header_ops = {
449 .create = dlci_header,
450};
451
452static const struct net_device_ops dlci_netdev_ops = {
453 .ndo_open = dlci_open,
454 .ndo_stop = dlci_close,
455 .ndo_do_ioctl = dlci_dev_ioctl,
456 .ndo_start_xmit = dlci_transmit,
457 .ndo_change_mtu = dlci_change_mtu,
458};
459
460static void dlci_setup(struct net_device *dev)
461{
462 struct dlci_local *dlp = netdev_priv(dev);
463
464 dev->flags = 0;
465 dev->header_ops = &dlci_header_ops;
466 dev->netdev_ops = &dlci_netdev_ops;
467 dev->destructor = free_netdev;
468
469 dlp->receive = dlci_receive;
470
471 dev->type = ARPHRD_DLCI;
472 dev->hard_header_len = sizeof(struct frhdr);
473 dev->addr_len = sizeof(short);
474
475}
476
477/* if slave is unregistering, then cleanup master */
478static int dlci_dev_event(struct notifier_block *unused,
479 unsigned long event, void *ptr)
480{
481 struct net_device *dev = (struct net_device *) ptr;
482
483 if (dev_net(dev) != &init_net)
484 return NOTIFY_DONE;
485
486 if (event == NETDEV_UNREGISTER) {
487 struct dlci_local *dlp;
488
489 list_for_each_entry(dlp, &dlci_devs, list) {
490 if (dlp->slave == dev) {
491 list_del(&dlp->list);
492 unregister_netdevice(dlp->master);
493 dev_put(dlp->slave);
494 break;
495 }
496 }
497 }
498 return NOTIFY_DONE;
499}
500
501static struct notifier_block dlci_notifier = {
502 .notifier_call = dlci_dev_event,
503};
504
505static int __init init_dlci(void)
506{
507 dlci_ioctl_set(dlci_ioctl);
508 register_netdevice_notifier(&dlci_notifier);
509
510 printk("%s.\n", version);
511
512 return 0;
513}
514
515static void __exit dlci_exit(void)
516{
517 struct dlci_local *dlp, *nxt;
518
519 dlci_ioctl_set(NULL);
520 unregister_netdevice_notifier(&dlci_notifier);
521
522 rtnl_lock();
523 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
524 unregister_netdevice(dlp->master);
525 dev_put(dlp->slave);
526 }
527 rtnl_unlock();
528}
529
530module_init(init_dlci);
531module_exit(dlci_exit);
532
533MODULE_AUTHOR("Mike McLagan");
534MODULE_DESCRIPTION("Frame Relay DLCI layer");
535MODULE_LICENSE("GPL");
1/*
2 * DLCI Implementation of Frame Relay protocol for Linux, according to
3 * RFC 1490. This generic device provides en/decapsulation for an
4 * underlying hardware driver. Routes & IPs are assigned to these
5 * interfaces. Requires 'dlcicfg' program to create usable
6 * interfaces, the initial one, 'dlci' is for IOCTL use only.
7 *
8 * Version: @(#)dlci.c 0.35 4 Jan 1997
9 *
10 * Author: Mike McLagan <mike.mclagan@linux.org>
11 *
12 * Changes:
13 *
14 * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
15 * DLCI_RET handling
16 * 0.20 Mike McLagan More conservative on which packets
17 * are returned for retry and which are
18 * are dropped. If DLCI_RET_DROP is
19 * returned from the FRAD, the packet is
20 * sent back to Linux for re-transmission
21 * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
22 * 0.30 Jim Freeman Fixed to allow IPX traffic
23 * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version
28 * 2 of the License, or (at your option) any later version.
29 */
30
31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33#include <linux/module.h>
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/fcntl.h>
37#include <linux/interrupt.h>
38#include <linux/ptrace.h>
39#include <linux/ioport.h>
40#include <linux/in.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/string.h>
44#include <linux/errno.h>
45#include <linux/netdevice.h>
46#include <linux/skbuff.h>
47#include <linux/if_arp.h>
48#include <linux/if_frad.h>
49#include <linux/bitops.h>
50
51#include <net/sock.h>
52
53#include <asm/io.h>
54#include <asm/dma.h>
55#include <asm/uaccess.h>
56
57static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
58
59static LIST_HEAD(dlci_devs);
60
61static void dlci_setup(struct net_device *);
62
63/*
64 * these encapsulate the RFC 1490 requirements as well as
65 * deal with packet transmission and reception, working with
66 * the upper network layers
67 */
68
69static int dlci_header(struct sk_buff *skb, struct net_device *dev,
70 unsigned short type, const void *daddr,
71 const void *saddr, unsigned len)
72{
73 struct frhdr hdr;
74 unsigned int hlen;
75 char *dest;
76
77 hdr.control = FRAD_I_UI;
78 switch (type)
79 {
80 case ETH_P_IP:
81 hdr.IP_NLPID = FRAD_P_IP;
82 hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
83 break;
84
85 /* feel free to add other types, if necessary */
86
87 default:
88 hdr.pad = FRAD_P_PADDING;
89 hdr.NLPID = FRAD_P_SNAP;
90 memset(hdr.OUI, 0, sizeof(hdr.OUI));
91 hdr.PID = htons(type);
92 hlen = sizeof(hdr);
93 break;
94 }
95
96 dest = skb_push(skb, hlen);
97 if (!dest)
98 return 0;
99
100 memcpy(dest, &hdr, hlen);
101
102 return hlen;
103}
104
105static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
106{
107 struct frhdr *hdr;
108 int process, header;
109
110 if (!pskb_may_pull(skb, sizeof(*hdr))) {
111 netdev_notice(dev, "invalid data no header\n");
112 dev->stats.rx_errors++;
113 kfree_skb(skb);
114 return;
115 }
116
117 hdr = (struct frhdr *) skb->data;
118 process = 0;
119 header = 0;
120 skb->dev = dev;
121
122 if (hdr->control != FRAD_I_UI)
123 {
124 netdev_notice(dev, "Invalid header flag 0x%02X\n",
125 hdr->control);
126 dev->stats.rx_errors++;
127 }
128 else
129 switch (hdr->IP_NLPID)
130 {
131 case FRAD_P_PADDING:
132 if (hdr->NLPID != FRAD_P_SNAP)
133 {
134 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
135 hdr->NLPID);
136 dev->stats.rx_errors++;
137 break;
138 }
139
140 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
141 {
142 netdev_notice(dev, "Unsupported organizationally unique identifier 0x%02X-%02X-%02X\n",
143 hdr->OUI[0],
144 hdr->OUI[1],
145 hdr->OUI[2]);
146 dev->stats.rx_errors++;
147 break;
148 }
149
150 /* at this point, it's an EtherType frame */
151 header = sizeof(struct frhdr);
152 /* Already in network order ! */
153 skb->protocol = hdr->PID;
154 process = 1;
155 break;
156
157 case FRAD_P_IP:
158 header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
159 skb->protocol = htons(ETH_P_IP);
160 process = 1;
161 break;
162
163 case FRAD_P_SNAP:
164 case FRAD_P_Q933:
165 case FRAD_P_CLNP:
166 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
167 hdr->pad);
168 dev->stats.rx_errors++;
169 break;
170
171 default:
172 netdev_notice(dev, "Invalid pad byte 0x%02X\n",
173 hdr->pad);
174 dev->stats.rx_errors++;
175 break;
176 }
177
178 if (process)
179 {
180 /* we've set up the protocol, so discard the header */
181 skb_reset_mac_header(skb);
182 skb_pull(skb, header);
183 dev->stats.rx_bytes += skb->len;
184 netif_rx(skb);
185 dev->stats.rx_packets++;
186 }
187 else
188 dev_kfree_skb(skb);
189}
190
191static netdev_tx_t dlci_transmit(struct sk_buff *skb, struct net_device *dev)
192{
193 struct dlci_local *dlp = netdev_priv(dev);
194
195 if (skb)
196 dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave);
197 return NETDEV_TX_OK;
198}
199
200static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
201{
202 struct dlci_conf config;
203 struct dlci_local *dlp;
204 struct frad_local *flp;
205 int err;
206
207 dlp = netdev_priv(dev);
208
209 flp = netdev_priv(dlp->slave);
210
211 if (!get)
212 {
213 if (copy_from_user(&config, conf, sizeof(struct dlci_conf)))
214 return -EFAULT;
215 if (config.flags & ~DLCI_VALID_FLAGS)
216 return -EINVAL;
217 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
218 dlp->configured = 1;
219 }
220
221 err = (*flp->dlci_conf)(dlp->slave, dev, get);
222 if (err)
223 return err;
224
225 if (get)
226 {
227 if (copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
228 return -EFAULT;
229 }
230
231 return 0;
232}
233
234static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
235{
236 struct dlci_local *dlp;
237
238 if (!capable(CAP_NET_ADMIN))
239 return -EPERM;
240
241 dlp = netdev_priv(dev);
242
243 switch (cmd)
244 {
245 case DLCI_GET_SLAVE:
246 if (!*(short *)(dev->dev_addr))
247 return -EINVAL;
248
249 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
250 break;
251
252 case DLCI_GET_CONF:
253 case DLCI_SET_CONF:
254 if (!*(short *)(dev->dev_addr))
255 return -EINVAL;
256
257 return dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF);
258 break;
259
260 default:
261 return -EOPNOTSUPP;
262 }
263 return 0;
264}
265
266static int dlci_change_mtu(struct net_device *dev, int new_mtu)
267{
268 struct dlci_local *dlp = netdev_priv(dev);
269
270 return dev_set_mtu(dlp->slave, new_mtu);
271}
272
273static int dlci_open(struct net_device *dev)
274{
275 struct dlci_local *dlp;
276 struct frad_local *flp;
277 int err;
278
279 dlp = netdev_priv(dev);
280
281 if (!*(short *)(dev->dev_addr))
282 return -EINVAL;
283
284 if (!netif_running(dlp->slave))
285 return -ENOTCONN;
286
287 flp = netdev_priv(dlp->slave);
288 err = (*flp->activate)(dlp->slave, dev);
289 if (err)
290 return err;
291
292 netif_start_queue(dev);
293
294 return 0;
295}
296
297static int dlci_close(struct net_device *dev)
298{
299 struct dlci_local *dlp;
300 struct frad_local *flp;
301 int err;
302
303 netif_stop_queue(dev);
304
305 dlp = netdev_priv(dev);
306
307 flp = netdev_priv(dlp->slave);
308 err = (*flp->deactivate)(dlp->slave, dev);
309
310 return 0;
311}
312
313static int dlci_add(struct dlci_add *dlci)
314{
315 struct net_device *master, *slave;
316 struct dlci_local *dlp;
317 struct frad_local *flp;
318 int err = -EINVAL;
319
320
321 /* validate slave device */
322 slave = dev_get_by_name(&init_net, dlci->devname);
323 if (!slave)
324 return -ENODEV;
325
326 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
327 goto err1;
328
329 /* create device name */
330 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
331 dlci_setup);
332 if (!master) {
333 err = -ENOMEM;
334 goto err1;
335 }
336
337 /* make sure same slave not already registered */
338 rtnl_lock();
339 list_for_each_entry(dlp, &dlci_devs, list) {
340 if (dlp->slave == slave) {
341 err = -EBUSY;
342 goto err2;
343 }
344 }
345
346 *(short *)(master->dev_addr) = dlci->dlci;
347
348 dlp = netdev_priv(master);
349 dlp->slave = slave;
350 dlp->master = master;
351
352 flp = netdev_priv(slave);
353 err = (*flp->assoc)(slave, master);
354 if (err < 0)
355 goto err2;
356
357 err = register_netdevice(master);
358 if (err < 0)
359 goto err2;
360
361 strcpy(dlci->devname, master->name);
362
363 list_add(&dlp->list, &dlci_devs);
364 rtnl_unlock();
365
366 return 0;
367
368 err2:
369 rtnl_unlock();
370 free_netdev(master);
371 err1:
372 dev_put(slave);
373 return err;
374}
375
376static int dlci_del(struct dlci_add *dlci)
377{
378 struct dlci_local *dlp;
379 struct frad_local *flp;
380 struct net_device *master, *slave;
381 int err;
382 bool found = false;
383
384 rtnl_lock();
385
386 /* validate slave device */
387 master = __dev_get_by_name(&init_net, dlci->devname);
388 if (!master) {
389 err = -ENODEV;
390 goto out;
391 }
392
393 list_for_each_entry(dlp, &dlci_devs, list) {
394 if (dlp->master == master) {
395 found = true;
396 break;
397 }
398 }
399 if (!found) {
400 err = -ENODEV;
401 goto out;
402 }
403
404 if (netif_running(master)) {
405 err = -EBUSY;
406 goto out;
407 }
408
409 dlp = netdev_priv(master);
410 slave = dlp->slave;
411 flp = netdev_priv(slave);
412
413 err = (*flp->deassoc)(slave, master);
414 if (!err) {
415 list_del(&dlp->list);
416
417 unregister_netdevice(master);
418
419 dev_put(slave);
420 }
421out:
422 rtnl_unlock();
423 return err;
424}
425
426static int dlci_ioctl(unsigned int cmd, void __user *arg)
427{
428 struct dlci_add add;
429 int err;
430
431 if (!capable(CAP_NET_ADMIN))
432 return -EPERM;
433
434 if (copy_from_user(&add, arg, sizeof(struct dlci_add)))
435 return -EFAULT;
436
437 switch (cmd)
438 {
439 case SIOCADDDLCI:
440 err = dlci_add(&add);
441
442 if (!err)
443 if (copy_to_user(arg, &add, sizeof(struct dlci_add)))
444 return -EFAULT;
445 break;
446
447 case SIOCDELDLCI:
448 err = dlci_del(&add);
449 break;
450
451 default:
452 err = -EINVAL;
453 }
454
455 return err;
456}
457
458static const struct header_ops dlci_header_ops = {
459 .create = dlci_header,
460};
461
462static const struct net_device_ops dlci_netdev_ops = {
463 .ndo_open = dlci_open,
464 .ndo_stop = dlci_close,
465 .ndo_do_ioctl = dlci_dev_ioctl,
466 .ndo_start_xmit = dlci_transmit,
467 .ndo_change_mtu = dlci_change_mtu,
468};
469
470static void dlci_setup(struct net_device *dev)
471{
472 struct dlci_local *dlp = netdev_priv(dev);
473
474 dev->flags = 0;
475 dev->header_ops = &dlci_header_ops;
476 dev->netdev_ops = &dlci_netdev_ops;
477 dev->destructor = free_netdev;
478
479 dlp->receive = dlci_receive;
480
481 dev->type = ARPHRD_DLCI;
482 dev->hard_header_len = sizeof(struct frhdr);
483 dev->addr_len = sizeof(short);
484
485}
486
487/* if slave is unregistering, then cleanup master */
488static int dlci_dev_event(struct notifier_block *unused,
489 unsigned long event, void *ptr)
490{
491 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
492
493 if (dev_net(dev) != &init_net)
494 return NOTIFY_DONE;
495
496 if (event == NETDEV_UNREGISTER) {
497 struct dlci_local *dlp;
498
499 list_for_each_entry(dlp, &dlci_devs, list) {
500 if (dlp->slave == dev) {
501 list_del(&dlp->list);
502 unregister_netdevice(dlp->master);
503 dev_put(dlp->slave);
504 break;
505 }
506 }
507 }
508 return NOTIFY_DONE;
509}
510
511static struct notifier_block dlci_notifier = {
512 .notifier_call = dlci_dev_event,
513};
514
515static int __init init_dlci(void)
516{
517 dlci_ioctl_set(dlci_ioctl);
518 register_netdevice_notifier(&dlci_notifier);
519
520 printk("%s.\n", version);
521
522 return 0;
523}
524
525static void __exit dlci_exit(void)
526{
527 struct dlci_local *dlp, *nxt;
528
529 dlci_ioctl_set(NULL);
530 unregister_netdevice_notifier(&dlci_notifier);
531
532 rtnl_lock();
533 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
534 unregister_netdevice(dlp->master);
535 dev_put(dlp->slave);
536 }
537 rtnl_unlock();
538}
539
540module_init(init_dlci);
541module_exit(dlci_exit);
542
543MODULE_AUTHOR("Mike McLagan");
544MODULE_DESCRIPTION("Frame Relay DLCI layer");
545MODULE_LICENSE("GPL");