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 | |
| 53 | #include <asm/system.h> |
| 54 | #include <asm/io.h> |
| 55 | #include <asm/dma.h> |
| 56 | #include <asm/uaccess.h> |
| 57 | |
| 58 | static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org"; |
| 59 | |
| 60 | static LIST_HEAD(dlci_devs); |
| 61 | |
| 62 | static 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 | |
| 70 | static int dlci_header(struct sk_buff *skb, struct net_device *dev, |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 71 | unsigned short type, const void *daddr, |
| 72 | const void *saddr, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | struct frhdr hdr; |
| 75 | struct dlci_local *dlp; |
| 76 | unsigned int hlen; |
| 77 | char *dest; |
| 78 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 79 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | hdr.control = FRAD_I_UI; |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 82 | switch (type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 102 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | memcpy(dest, &hdr, hlen); |
| 105 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 106 | return hlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static 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 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 115 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (!pskb_may_pull(skb, sizeof(*hdr))) { |
Joe Perches | 86fb0cc | 2011-06-26 19:01:31 +0000 | [diff] [blame] | 117 | netdev_notice(dev, "invalid data no header\n"); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 118 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Joe Perches | 86fb0cc | 2011-06-26 19:01:31 +0000 | [diff] [blame] | 130 | netdev_notice(dev, "Invalid header flag 0x%02X\n", |
| 131 | hdr->control); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 132 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | else |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 135 | switch (hdr->IP_NLPID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | case FRAD_P_PADDING: |
| 138 | if (hdr->NLPID != FRAD_P_SNAP) |
| 139 | { |
Joe Perches | 86fb0cc | 2011-06-26 19:01:31 +0000 | [diff] [blame] | 140 | netdev_notice(dev, "Unsupported NLPID 0x%02X\n", |
| 141 | hdr->NLPID); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 142 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | |
| 146 | if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0) |
| 147 | { |
Joe Perches | 86fb0cc | 2011-06-26 19:01:31 +0000 | [diff] [blame] | 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]); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 152 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Joe Perches | 86fb0cc | 2011-06-26 19:01:31 +0000 | [diff] [blame] | 172 | netdev_notice(dev, "Unsupported NLPID 0x%02X\n", |
| 173 | hdr->pad); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 174 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | default: |
Joe Perches | 86fb0cc | 2011-06-26 19:01:31 +0000 | [diff] [blame] | 178 | netdev_notice(dev, "Invalid pad byte 0x%02X\n", |
| 179 | hdr->pad); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 180 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | |
| 184 | if (process) |
| 185 | { |
| 186 | /* we've set up the protocol, so discard the header */ |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 187 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | skb_pull(skb, header); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 189 | dev->stats.rx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | netif_rx(skb); |
Stephen Hemminger | 07d117c | 2009-03-20 19:36:14 +0000 | [diff] [blame] | 191 | dev->stats.rx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | else |
| 194 | dev_kfree_skb(skb); |
| 195 | } |
| 196 | |
Stephen Hemminger | 3848242 | 2009-09-04 05:33:46 +0000 | [diff] [blame] | 197 | 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] | 198 | { |
Stephen Hemminger | 3848242 | 2009-09-04 05:33:46 +0000 | [diff] [blame] | 199 | struct dlci_local *dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Stephen Hemminger | 3848242 | 2009-09-04 05:33:46 +0000 | [diff] [blame] | 201 | if (skb) |
| 202 | dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave); |
| 203 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static 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 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 213 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 215 | flp = netdev_priv(dlp->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | if (!get) |
| 218 | { |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 219 | if (copy_from_user(&config, conf, sizeof(struct dlci_conf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return -EFAULT; |
| 221 | if (config.flags & ~DLCI_VALID_FLAGS) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 222 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 229 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | if (get) |
| 232 | { |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 233 | if (copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return -EFAULT; |
| 235 | } |
| 236 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 237 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static 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)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 245 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 247 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 249 | switch (cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | case DLCI_GET_SLAVE: |
| 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 | |
| 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)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 261 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 263 | return dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | default: |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 267 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 269 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static int dlci_change_mtu(struct net_device *dev, int new_mtu) |
| 273 | { |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 274 | struct dlci_local *dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 276 | return dev_set_mtu(dlp->slave, new_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int dlci_open(struct net_device *dev) |
| 280 | { |
| 281 | struct dlci_local *dlp; |
| 282 | struct frad_local *flp; |
| 283 | int err; |
| 284 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 285 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | if (!*(short *)(dev->dev_addr)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 288 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | if (!netif_running(dlp->slave)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 291 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 293 | flp = netdev_priv(dlp->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | err = (*flp->activate)(dlp->slave, dev); |
| 295 | if (err) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 296 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | netif_start_queue(dev); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static 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 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 311 | dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 313 | flp = netdev_priv(dlp->slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | err = (*flp->deactivate)(dlp->slave, dev); |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | static 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 */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 328 | slave = dev_get_by_name(&init_net, dlci->devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (!slave) |
| 330 | return -ENODEV; |
| 331 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 332 | if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | *(short *)(master->dev_addr) = dlci->dlci; |
| 353 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 354 | dlp = netdev_priv(master); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | dlp->slave = slave; |
| 356 | dlp->master = master; |
| 357 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 358 | flp = netdev_priv(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 372 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | err2: |
| 375 | rtnl_unlock(); |
| 376 | free_netdev(master); |
| 377 | err1: |
| 378 | dev_put(slave); |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 379 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static 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 */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 390 | master = __dev_get_by_name(&init_net, dlci->devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (!master) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 392 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
| 394 | if (netif_running(master)) { |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 395 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 398 | dlp = netdev_priv(master); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | slave = dlp->slave; |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 400 | flp = netdev_priv(slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 413 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static 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)) |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 422 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 424 | if (copy_from_user(&add, arg, sizeof(struct dlci_add))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return -EFAULT; |
| 426 | |
| 427 | switch (cmd) |
| 428 | { |
| 429 | case SIOCADDDLCI: |
| 430 | err = dlci_add(&add); |
| 431 | |
| 432 | if (!err) |
Rudy Matela | 48b3d3e | 2009-11-29 23:42:14 -0800 | [diff] [blame] | 433 | if (copy_to_user(arg, &add, sizeof(struct dlci_add))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return -EFAULT; |
| 435 | break; |
| 436 | |
| 437 | case SIOCDELDLCI: |
| 438 | err = dlci_del(&add); |
| 439 | break; |
| 440 | |
| 441 | default: |
| 442 | err = -EINVAL; |
| 443 | } |
| 444 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 445 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 448 | static const struct header_ops dlci_header_ops = { |
| 449 | .create = dlci_header, |
| 450 | }; |
| 451 | |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 452 | static 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | static void dlci_setup(struct net_device *dev) |
| 461 | { |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 462 | struct dlci_local *dlp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | dev->flags = 0; |
Stephen Hemminger | 3b04ddd | 2007-10-09 01:40:57 -0700 | [diff] [blame] | 465 | dev->header_ops = &dlci_header_ops; |
Stephen Hemminger | 49e8aba | 2009-03-20 19:36:15 +0000 | [diff] [blame] | 466 | dev->netdev_ops = &dlci_netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
| 478 | static 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 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 483 | if (dev_net(dev) != &init_net) |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 484 | return NOTIFY_DONE; |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 501 | static struct notifier_block dlci_notifier = { |
| 502 | .notifier_call = dlci_dev_event, |
| 503 | }; |
| 504 | |
| 505 | static 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 | |
| 515 | static 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 | |
| 530 | module_init(init_dlci); |
| 531 | module_exit(dlci_exit); |
| 532 | |
| 533 | MODULE_AUTHOR("Mike McLagan"); |
| 534 | MODULE_DESCRIPTION("Frame Relay DLCI layer"); |
| 535 | MODULE_LICENSE("GPL"); |