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