Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/types.h> |
| 34 | #include <linux/fcntl.h> |
| 35 | #include <linux/interrupt.h> |
| 36 | #include <linux/ptrace.h> |
| 37 | #include <linux/ioport.h> |
| 38 | #include <linux/in.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/netdevice.h> |
| 44 | #include <linux/skbuff.h> |
| 45 | #include <linux/if_arp.h> |
| 46 | #include <linux/if_frad.h> |
| 47 | #include <linux/bitops.h> |
| 48 | |
| 49 | #include <net/sock.h> |
| 50 | |
| 51 | #include <asm/system.h> |
| 52 | #include <asm/io.h> |
| 53 | #include <asm/dma.h> |
| 54 | #include <asm/uaccess.h> |
| 55 | |
| 56 | static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org"; |
| 57 | |
| 58 | static LIST_HEAD(dlci_devs); |
| 59 | |
| 60 | static void dlci_setup(struct net_device *); |
| 61 | |
| 62 | /* |
| 63 | * these encapsulate the RFC 1490 requirements as well as |
| 64 | * deal with packet transmission and reception, working with |
| 65 | * the upper network layers |
| 66 | */ |
| 67 | |
| 68 | static int dlci_header(struct sk_buff *skb, struct net_device *dev, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 69 | unsigned short type, const void *daddr, |
| 70 | const void *saddr, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | struct frhdr hdr; |
| 73 | struct dlci_local *dlp; |
| 74 | unsigned int hlen; |
| 75 | char *dest; |
| 76 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 77 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | hdr.control = FRAD_I_UI; |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 80 | switch (type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | case ETH_P_IP: |
| 83 | hdr.IP_NLPID = FRAD_P_IP; |
| 84 | hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID); |
| 85 | break; |
| 86 | |
| 87 | /* feel free to add other types, if necessary */ |
| 88 | |
| 89 | default: |
| 90 | hdr.pad = FRAD_P_PADDING; |
| 91 | hdr.NLPID = FRAD_P_SNAP; |
| 92 | memset(hdr.OUI, 0, sizeof(hdr.OUI)); |
| 93 | hdr.PID = htons(type); |
| 94 | hlen = sizeof(hdr); |
| 95 | break; |
| 96 | } |
| 97 | |
| 98 | dest = skb_push(skb, hlen); |
| 99 | if (!dest) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 100 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | memcpy(dest, &hdr, hlen); |
| 103 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 104 | return hlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void dlci_receive(struct sk_buff *skb, struct net_device *dev) |
| 108 | { |
| 109 | struct dlci_local *dlp; |
| 110 | struct frhdr *hdr; |
| 111 | int process, header; |
| 112 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 113 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (!pskb_may_pull(skb, sizeof(*hdr))) { |
| 115 | printk(KERN_NOTICE "%s: invalid data no header\n", |
| 116 | dev->name); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 117 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | kfree_skb(skb); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | hdr = (struct frhdr *) skb->data; |
| 123 | process = 0; |
| 124 | header = 0; |
| 125 | skb->dev = dev; |
| 126 | |
| 127 | if (hdr->control != FRAD_I_UI) |
| 128 | { |
| 129 | printk(KERN_NOTICE "%s: Invalid header flag 0x%02X.\n", dev->name, hdr->control); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 130 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | else |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 133 | switch (hdr->IP_NLPID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | case FRAD_P_PADDING: |
| 136 | if (hdr->NLPID != FRAD_P_SNAP) |
| 137 | { |
| 138 | printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->NLPID); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 139 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | |
| 143 | if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0) |
| 144 | { |
| 145 | printk(KERN_NOTICE "%s: Unsupported organizationally unique identifier 0x%02X-%02X-%02X.\n", dev->name, hdr->OUI[0], hdr->OUI[1], hdr->OUI[2]); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 146 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->pad); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 167 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | break; |
| 169 | |
| 170 | default: |
| 171 | printk(KERN_NOTICE "%s: Invalid pad byte 0x%02X.\n", dev->name, hdr->pad); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 172 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | |
| 176 | if (process) |
| 177 | { |
| 178 | /* we've set up the protocol, so discard the header */ |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 179 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | skb_pull(skb, header); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 181 | dev->stats.rx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | netif_rx(skb); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 183 | dev->stats.rx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | dev_kfree_skb(skb); |
| 187 | } |
| 188 | |
Stephen Hemminger | 3848242 | 2009-09-04 05:33:46 +0000 | [diff] [blame] | 189 | static netdev_tx_t dlci_transmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Stephen Hemminger | 3848242 | 2009-09-04 05:33:46 +0000 | [diff] [blame] | 191 | struct dlci_local *dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Stephen Hemminger | 3848242 | 2009-09-04 05:33:46 +0000 | [diff] [blame] | 193 | if (skb) |
| 194 | dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave); |
| 195 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get) |
| 199 | { |
| 200 | struct dlci_conf config; |
| 201 | struct dlci_local *dlp; |
| 202 | struct frad_local *flp; |
| 203 | int err; |
| 204 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 205 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 207 | flp = netdev_priv(dlp->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (!get) |
| 210 | { |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 211 | if (copy_from_user(&config, conf, sizeof(struct dlci_conf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return -EFAULT; |
| 213 | if (config.flags & ~DLCI_VALID_FLAGS) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 214 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | memcpy(&dlp->config, &config, sizeof(struct dlci_conf)); |
| 216 | dlp->configured = 1; |
| 217 | } |
| 218 | |
| 219 | err = (*flp->dlci_conf)(dlp->slave, dev, get); |
| 220 | if (err) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 221 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | if (get) |
| 224 | { |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 225 | if (copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return -EFAULT; |
| 227 | } |
| 228 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 229 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 233 | { |
| 234 | struct dlci_local *dlp; |
| 235 | |
| 236 | if (!capable(CAP_NET_ADMIN)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 237 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 239 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 241 | switch (cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | case DLCI_GET_SLAVE: |
| 244 | if (!*(short *)(dev->dev_addr)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 245 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave)); |
| 248 | break; |
| 249 | |
| 250 | case DLCI_GET_CONF: |
| 251 | case DLCI_SET_CONF: |
| 252 | if (!*(short *)(dev->dev_addr)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 253 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 255 | return dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | break; |
| 257 | |
| 258 | default: |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 259 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 261 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int dlci_change_mtu(struct net_device *dev, int new_mtu) |
| 265 | { |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 266 | struct dlci_local *dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 268 | return dev_set_mtu(dlp->slave, new_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static int dlci_open(struct net_device *dev) |
| 272 | { |
| 273 | struct dlci_local *dlp; |
| 274 | struct frad_local *flp; |
| 275 | int err; |
| 276 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 277 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | if (!*(short *)(dev->dev_addr)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 280 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
| 282 | if (!netif_running(dlp->slave)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 283 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 285 | flp = netdev_priv(dlp->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | err = (*flp->activate)(dlp->slave, dev); |
| 287 | if (err) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 288 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | netif_start_queue(dev); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int dlci_close(struct net_device *dev) |
| 296 | { |
| 297 | struct dlci_local *dlp; |
| 298 | struct frad_local *flp; |
| 299 | int err; |
| 300 | |
| 301 | netif_stop_queue(dev); |
| 302 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 303 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 305 | flp = netdev_priv(dlp->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | err = (*flp->deactivate)(dlp->slave, dev); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | static int dlci_add(struct dlci_add *dlci) |
| 312 | { |
| 313 | struct net_device *master, *slave; |
| 314 | struct dlci_local *dlp; |
| 315 | struct frad_local *flp; |
| 316 | int err = -EINVAL; |
| 317 | |
| 318 | |
| 319 | /* validate slave device */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 320 | slave = dev_get_by_name(&init_net, dlci->devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | if (!slave) |
| 322 | return -ENODEV; |
| 323 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 324 | if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | goto err1; |
| 326 | |
| 327 | /* create device name */ |
| 328 | master = alloc_netdev( sizeof(struct dlci_local), "dlci%d", |
| 329 | dlci_setup); |
| 330 | if (!master) { |
| 331 | err = -ENOMEM; |
| 332 | goto err1; |
| 333 | } |
| 334 | |
| 335 | /* make sure same slave not already registered */ |
| 336 | rtnl_lock(); |
| 337 | list_for_each_entry(dlp, &dlci_devs, list) { |
| 338 | if (dlp->slave == slave) { |
| 339 | err = -EBUSY; |
| 340 | goto err2; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | err = dev_alloc_name(master, master->name); |
| 345 | if (err < 0) |
| 346 | goto err2; |
| 347 | |
| 348 | *(short *)(master->dev_addr) = dlci->dlci; |
| 349 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 350 | dlp = netdev_priv(master); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | dlp->slave = slave; |
| 352 | dlp->master = master; |
| 353 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 354 | flp = netdev_priv(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | err = (*flp->assoc)(slave, master); |
| 356 | if (err < 0) |
| 357 | goto err2; |
| 358 | |
| 359 | err = register_netdevice(master); |
| 360 | if (err < 0) |
| 361 | goto err2; |
| 362 | |
| 363 | strcpy(dlci->devname, master->name); |
| 364 | |
| 365 | list_add(&dlp->list, &dlci_devs); |
| 366 | rtnl_unlock(); |
| 367 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 368 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | err2: |
| 371 | rtnl_unlock(); |
| 372 | free_netdev(master); |
| 373 | err1: |
| 374 | dev_put(slave); |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 375 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int dlci_del(struct dlci_add *dlci) |
| 379 | { |
| 380 | struct dlci_local *dlp; |
| 381 | struct frad_local *flp; |
| 382 | struct net_device *master, *slave; |
| 383 | int err; |
| 384 | |
| 385 | /* validate slave device */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 386 | master = __dev_get_by_name(&init_net, dlci->devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (!master) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 388 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | if (netif_running(master)) { |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 391 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 394 | dlp = netdev_priv(master); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | slave = dlp->slave; |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 396 | flp = netdev_priv(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | rtnl_lock(); |
| 399 | err = (*flp->deassoc)(slave, master); |
| 400 | if (!err) { |
| 401 | list_del(&dlp->list); |
| 402 | |
| 403 | unregister_netdevice(master); |
| 404 | |
| 405 | dev_put(slave); |
| 406 | } |
| 407 | rtnl_unlock(); |
| 408 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 409 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static int dlci_ioctl(unsigned int cmd, void __user *arg) |
| 413 | { |
| 414 | struct dlci_add add; |
| 415 | int err; |
| 416 | |
| 417 | if (!capable(CAP_NET_ADMIN)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 418 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 420 | if (copy_from_user(&add, arg, sizeof(struct dlci_add))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return -EFAULT; |
| 422 | |
| 423 | switch (cmd) |
| 424 | { |
| 425 | case SIOCADDDLCI: |
| 426 | err = dlci_add(&add); |
| 427 | |
| 428 | if (!err) |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 429 | if (copy_to_user(arg, &add, sizeof(struct dlci_add))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | return -EFAULT; |
| 431 | break; |
| 432 | |
| 433 | case SIOCDELDLCI: |
| 434 | err = dlci_del(&add); |
| 435 | break; |
| 436 | |
| 437 | default: |
| 438 | err = -EINVAL; |
| 439 | } |
| 440 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 441 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 444 | static const struct header_ops dlci_header_ops = { |
| 445 | .create = dlci_header, |
| 446 | }; |
| 447 | |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 448 | static const struct net_device_ops dlci_netdev_ops = { |
| 449 | .ndo_open = dlci_open, |
| 450 | .ndo_stop = dlci_close, |
| 451 | .ndo_do_ioctl = dlci_dev_ioctl, |
| 452 | .ndo_start_xmit = dlci_transmit, |
| 453 | .ndo_change_mtu = dlci_change_mtu, |
| 454 | }; |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | static void dlci_setup(struct net_device *dev) |
| 457 | { |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 458 | struct dlci_local *dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | dev->flags = 0; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 461 | dev->header_ops = &dlci_header_ops; |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 462 | dev->netdev_ops = &dlci_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | dev->destructor = free_netdev; |
| 464 | |
| 465 | dlp->receive = dlci_receive; |
| 466 | |
| 467 | dev->type = ARPHRD_DLCI; |
| 468 | dev->hard_header_len = sizeof(struct frhdr); |
| 469 | dev->addr_len = sizeof(short); |
| 470 | |
| 471 | } |
| 472 | |
| 473 | /* if slave is unregistering, then cleanup master */ |
| 474 | static int dlci_dev_event(struct notifier_block *unused, |
| 475 | unsigned long event, void *ptr) |
| 476 | { |
| 477 | struct net_device *dev = (struct net_device *) ptr; |
| 478 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 479 | if (dev_net(dev) != &init_net) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 480 | return NOTIFY_DONE; |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | if (event == NETDEV_UNREGISTER) { |
| 483 | struct dlci_local *dlp; |
| 484 | |
| 485 | list_for_each_entry(dlp, &dlci_devs, list) { |
| 486 | if (dlp->slave == dev) { |
| 487 | list_del(&dlp->list); |
| 488 | unregister_netdevice(dlp->master); |
| 489 | dev_put(dlp->slave); |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | return NOTIFY_DONE; |
| 495 | } |
| 496 | |
| 497 | static struct notifier_block dlci_notifier = { |
| 498 | .notifier_call = dlci_dev_event, |
| 499 | }; |
| 500 | |
| 501 | static int __init init_dlci(void) |
| 502 | { |
| 503 | dlci_ioctl_set(dlci_ioctl); |
| 504 | register_netdevice_notifier(&dlci_notifier); |
| 505 | |
| 506 | printk("%s.\n", version); |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static void __exit dlci_exit(void) |
| 512 | { |
| 513 | struct dlci_local *dlp, *nxt; |
| 514 | |
| 515 | dlci_ioctl_set(NULL); |
| 516 | unregister_netdevice_notifier(&dlci_notifier); |
| 517 | |
| 518 | rtnl_lock(); |
| 519 | list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) { |
| 520 | unregister_netdevice(dlp->master); |
| 521 | dev_put(dlp->slave); |
| 522 | } |
| 523 | rtnl_unlock(); |
| 524 | } |
| 525 | |
| 526 | module_init(init_dlci); |
| 527 | module_exit(dlci_exit); |
| 528 | |
| 529 | MODULE_AUTHOR("Mike McLagan"); |
| 530 | MODULE_DESCRIPTION("Frame Relay DLCI layer"); |
| 531 | MODULE_LICENSE("GPL"); |