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