Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * Frame Relay support |
| 4 | * |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 5 | * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of version 2 of the GNU General Public License |
| 9 | * as published by the Free Software Foundation. |
| 10 | * |
| 11 | |
| 12 | Theory of PVC state |
| 13 | |
| 14 | DCE mode: |
| 15 | |
| 16 | (exist,new) -> 0,0 when "PVC create" or if "link unreliable" |
| 17 | 0,x -> 1,1 if "link reliable" when sending FULL STATUS |
| 18 | 1,1 -> 1,0 if received FULL STATUS ACK |
| 19 | |
| 20 | (active) -> 0 when "ifconfig PVC down" or "link unreliable" or "PVC create" |
| 21 | -> 1 when "PVC up" and (exist,new) = 1,0 |
| 22 | |
| 23 | DTE mode: |
| 24 | (exist,new,active) = FULL STATUS if "link reliable" |
| 25 | = 0, 0, 0 if "link unreliable" |
| 26 | No LMI: |
| 27 | active = open and "link reliable" |
| 28 | exist = new = not used |
| 29 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 30 | CCITT LMI: ITU-T Q.933 Annex A |
| 31 | ANSI LMI: ANSI T1.617 Annex D |
| 32 | CISCO LMI: the original, aka "Gang of Four" LMI |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/etherdevice.h> |
| 38 | #include <linux/hdlc.h> |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 39 | #include <linux/if_arp.h> |
| 40 | #include <linux/inetdevice.h> |
| 41 | #include <linux/init.h> |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/module.h> |
| 44 | #include <linux/pkt_sched.h> |
| 45 | #include <linux/poll.h> |
| 46 | #include <linux/rtnetlink.h> |
| 47 | #include <linux/skbuff.h> |
| 48 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #undef DEBUG_PKT |
| 51 | #undef DEBUG_ECN |
| 52 | #undef DEBUG_LINK |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 53 | #undef DEBUG_PROTO |
| 54 | #undef DEBUG_PVC |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 56 | #define FR_UI 0x03 |
| 57 | #define FR_PAD 0x00 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 59 | #define NLPID_IP 0xCC |
| 60 | #define NLPID_IPV6 0x8E |
| 61 | #define NLPID_SNAP 0x80 |
| 62 | #define NLPID_PAD 0x00 |
| 63 | #define NLPID_CCITT_ANSI_LMI 0x08 |
| 64 | #define NLPID_CISCO_LMI 0x09 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 67 | #define LMI_CCITT_ANSI_DLCI 0 /* LMI DLCI */ |
| 68 | #define LMI_CISCO_DLCI 1023 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 70 | #define LMI_CALLREF 0x00 /* Call Reference */ |
| 71 | #define LMI_ANSI_LOCKSHIFT 0x95 /* ANSI locking shift */ |
| 72 | #define LMI_ANSI_CISCO_REPTYPE 0x01 /* report type */ |
| 73 | #define LMI_CCITT_REPTYPE 0x51 |
| 74 | #define LMI_ANSI_CISCO_ALIVE 0x03 /* keep alive */ |
| 75 | #define LMI_CCITT_ALIVE 0x53 |
| 76 | #define LMI_ANSI_CISCO_PVCSTAT 0x07 /* PVC status */ |
| 77 | #define LMI_CCITT_PVCSTAT 0x57 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 79 | #define LMI_FULLREP 0x00 /* full report */ |
| 80 | #define LMI_INTEGRITY 0x01 /* link integrity report */ |
| 81 | #define LMI_SINGLE 0x02 /* single PVC report */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #define LMI_STATUS_ENQUIRY 0x75 |
| 84 | #define LMI_STATUS 0x7D /* reply */ |
| 85 | |
| 86 | #define LMI_REPT_LEN 1 /* report type element length */ |
| 87 | #define LMI_INTEG_LEN 2 /* link integrity element length */ |
| 88 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 89 | #define LMI_CCITT_CISCO_LENGTH 13 /* LMI frame lengths */ |
| 90 | #define LMI_ANSI_LENGTH 14 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | typedef struct { |
| 94 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 95 | unsigned ea1: 1; |
| 96 | unsigned cr: 1; |
| 97 | unsigned dlcih: 6; |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | unsigned ea2: 1; |
| 100 | unsigned de: 1; |
| 101 | unsigned becn: 1; |
| 102 | unsigned fecn: 1; |
| 103 | unsigned dlcil: 4; |
| 104 | #else |
| 105 | unsigned dlcih: 6; |
| 106 | unsigned cr: 1; |
| 107 | unsigned ea1: 1; |
| 108 | |
| 109 | unsigned dlcil: 4; |
| 110 | unsigned fecn: 1; |
| 111 | unsigned becn: 1; |
| 112 | unsigned de: 1; |
| 113 | unsigned ea2: 1; |
| 114 | #endif |
Eric Dumazet | ba2d358 | 2010-06-02 18:10:09 +0000 | [diff] [blame] | 115 | }__packed fr_hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 118 | typedef struct pvc_device_struct { |
| 119 | struct net_device *frad; |
| 120 | struct net_device *main; |
| 121 | struct net_device *ether; /* bridged Ethernet interface */ |
| 122 | struct pvc_device_struct *next; /* Sorted in ascending DLCI order */ |
| 123 | int dlci; |
| 124 | int open_count; |
| 125 | |
| 126 | struct { |
| 127 | unsigned int new: 1; |
| 128 | unsigned int active: 1; |
| 129 | unsigned int exist: 1; |
| 130 | unsigned int deleted: 1; |
| 131 | unsigned int fecn: 1; |
| 132 | unsigned int becn: 1; |
| 133 | unsigned int bandwidth; /* Cisco LMI reporting only */ |
| 134 | }state; |
| 135 | }pvc_device; |
| 136 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 137 | struct frad_state { |
| 138 | fr_proto settings; |
| 139 | pvc_device *first_pvc; |
| 140 | int dce_pvc_count; |
| 141 | |
| 142 | struct timer_list timer; |
| 143 | unsigned long last_poll; |
| 144 | int reliable; |
| 145 | int dce_changed; |
| 146 | int request; |
| 147 | int fullrep_sent; |
| 148 | u32 last_errors; /* last errors bit list */ |
| 149 | u8 n391cnt; |
| 150 | u8 txseq; /* TX sequence number */ |
| 151 | u8 rxseq; /* RX sequence number */ |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | static int fr_ioctl(struct net_device *dev, struct ifreq *ifr); |
| 156 | |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | static inline u16 q922_to_dlci(u8 *hdr) |
| 159 | { |
| 160 | return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4); |
| 161 | } |
| 162 | |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | static inline void dlci_to_q922(u8 *hdr, u16 dlci) |
| 165 | { |
| 166 | hdr[0] = (dlci >> 2) & 0xFC; |
| 167 | hdr[1] = ((dlci << 4) & 0xF0) | 0x01; |
| 168 | } |
| 169 | |
| 170 | |
Krzysztof Halasa | 983e230 | 2008-02-01 22:34:30 +0100 | [diff] [blame] | 171 | static inline struct frad_state* state(hdlc_device *hdlc) |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 172 | { |
| 173 | return(struct frad_state *)(hdlc->state); |
| 174 | } |
| 175 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | static inline pvc_device* find_pvc(hdlc_device *hdlc, u16 dlci) |
| 178 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 179 | pvc_device *pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | while (pvc) { |
| 182 | if (pvc->dlci == dlci) |
| 183 | return pvc; |
| 184 | if (pvc->dlci > dlci) |
Rudy Matela | 870571a | 2009-11-25 10:20:20 -0300 | [diff] [blame] | 185 | return NULL; /* the list is sorted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | pvc = pvc->next; |
| 187 | } |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 193 | static pvc_device* add_pvc(struct net_device *dev, u16 dlci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
| 195 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 196 | pvc_device *pvc, **pvc_p = &state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | while (*pvc_p) { |
| 199 | if ((*pvc_p)->dlci == dlci) |
| 200 | return *pvc_p; |
| 201 | if ((*pvc_p)->dlci > dlci) |
| 202 | break; /* the list is sorted */ |
| 203 | pvc_p = &(*pvc_p)->next; |
| 204 | } |
| 205 | |
Mariusz Kozlowski | 1ee3254 | 2007-08-10 15:24:50 -0700 | [diff] [blame] | 206 | pvc = kzalloc(sizeof(pvc_device), GFP_ATOMIC); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 207 | #ifdef DEBUG_PVC |
| 208 | printk(KERN_DEBUG "add_pvc: allocated pvc %p, frad %p\n", pvc, dev); |
| 209 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (!pvc) |
| 211 | return NULL; |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | pvc->dlci = dlci; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 214 | pvc->frad = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | pvc->next = *pvc_p; /* Put it in the chain */ |
| 216 | *pvc_p = pvc; |
| 217 | return pvc; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | static inline int pvc_is_used(pvc_device *pvc) |
| 222 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 223 | return pvc->main || pvc->ether; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | |
| 227 | static inline void pvc_carrier(int on, pvc_device *pvc) |
| 228 | { |
| 229 | if (on) { |
| 230 | if (pvc->main) |
| 231 | if (!netif_carrier_ok(pvc->main)) |
| 232 | netif_carrier_on(pvc->main); |
| 233 | if (pvc->ether) |
| 234 | if (!netif_carrier_ok(pvc->ether)) |
| 235 | netif_carrier_on(pvc->ether); |
| 236 | } else { |
| 237 | if (pvc->main) |
| 238 | if (netif_carrier_ok(pvc->main)) |
| 239 | netif_carrier_off(pvc->main); |
| 240 | if (pvc->ether) |
| 241 | if (netif_carrier_ok(pvc->ether)) |
| 242 | netif_carrier_off(pvc->ether); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
| 247 | static inline void delete_unused_pvcs(hdlc_device *hdlc) |
| 248 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 249 | pvc_device **pvc_p = &state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | while (*pvc_p) { |
| 252 | if (!pvc_is_used(*pvc_p)) { |
| 253 | pvc_device *pvc = *pvc_p; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 254 | #ifdef DEBUG_PVC |
| 255 | printk(KERN_DEBUG "freeing unused pvc: %p\n", pvc); |
| 256 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | *pvc_p = pvc->next; |
| 258 | kfree(pvc); |
| 259 | continue; |
| 260 | } |
| 261 | pvc_p = &(*pvc_p)->next; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | static inline struct net_device** get_dev_p(pvc_device *pvc, int type) |
| 267 | { |
| 268 | if (type == ARPHRD_ETHER) |
| 269 | return &pvc->ether; |
| 270 | else |
| 271 | return &pvc->main; |
| 272 | } |
| 273 | |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | static int fr_hard_header(struct sk_buff **skb_p, u16 dlci) |
| 276 | { |
| 277 | u16 head_len; |
| 278 | struct sk_buff *skb = *skb_p; |
| 279 | |
| 280 | switch (skb->protocol) { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 281 | case cpu_to_be16(NLPID_CCITT_ANSI_LMI): |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 282 | head_len = 4; |
| 283 | skb_push(skb, head_len); |
| 284 | skb->data[3] = NLPID_CCITT_ANSI_LMI; |
| 285 | break; |
| 286 | |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 287 | case cpu_to_be16(NLPID_CISCO_LMI): |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 288 | head_len = 4; |
| 289 | skb_push(skb, head_len); |
| 290 | skb->data[3] = NLPID_CISCO_LMI; |
| 291 | break; |
| 292 | |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 293 | case cpu_to_be16(ETH_P_IP): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | head_len = 4; |
| 295 | skb_push(skb, head_len); |
| 296 | skb->data[3] = NLPID_IP; |
| 297 | break; |
| 298 | |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 299 | case cpu_to_be16(ETH_P_IPV6): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | head_len = 4; |
| 301 | skb_push(skb, head_len); |
| 302 | skb->data[3] = NLPID_IPV6; |
| 303 | break; |
| 304 | |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 305 | case cpu_to_be16(ETH_P_802_3): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | head_len = 10; |
| 307 | if (skb_headroom(skb) < head_len) { |
| 308 | struct sk_buff *skb2 = skb_realloc_headroom(skb, |
| 309 | head_len); |
| 310 | if (!skb2) |
| 311 | return -ENOBUFS; |
| 312 | dev_kfree_skb(skb); |
| 313 | skb = *skb_p = skb2; |
| 314 | } |
| 315 | skb_push(skb, head_len); |
| 316 | skb->data[3] = FR_PAD; |
| 317 | skb->data[4] = NLPID_SNAP; |
| 318 | skb->data[5] = FR_PAD; |
| 319 | skb->data[6] = 0x80; |
| 320 | skb->data[7] = 0xC2; |
| 321 | skb->data[8] = 0x00; |
| 322 | skb->data[9] = 0x07; /* bridged Ethernet frame w/out FCS */ |
| 323 | break; |
| 324 | |
| 325 | default: |
| 326 | head_len = 10; |
| 327 | skb_push(skb, head_len); |
| 328 | skb->data[3] = FR_PAD; |
| 329 | skb->data[4] = NLPID_SNAP; |
| 330 | skb->data[5] = FR_PAD; |
| 331 | skb->data[6] = FR_PAD; |
| 332 | skb->data[7] = FR_PAD; |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 333 | *(__be16*)(skb->data + 8) = skb->protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | dlci_to_q922(skb->data, dlci); |
| 337 | skb->data[2] = FR_UI; |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | static int pvc_open(struct net_device *dev) |
| 344 | { |
Wang Chen | 2baf8a2 | 2008-11-21 16:34:18 -0800 | [diff] [blame] | 345 | pvc_device *pvc = dev->ml_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 347 | if ((pvc->frad->flags & IFF_UP) == 0) |
| 348 | return -EIO; /* Frad must be UP in order to activate PVC */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | if (pvc->open_count++ == 0) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 351 | hdlc_device *hdlc = dev_to_hdlc(pvc->frad); |
| 352 | if (state(hdlc)->settings.lmi == LMI_NONE) |
| 353 | pvc->state.active = netif_carrier_ok(pvc->frad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | pvc_carrier(pvc->state.active, pvc); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 356 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | static int pvc_close(struct net_device *dev) |
| 364 | { |
Wang Chen | 2baf8a2 | 2008-11-21 16:34:18 -0800 | [diff] [blame] | 365 | pvc_device *pvc = dev->ml_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | if (--pvc->open_count == 0) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 368 | hdlc_device *hdlc = dev_to_hdlc(pvc->frad); |
| 369 | if (state(hdlc)->settings.lmi == LMI_NONE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | pvc->state.active = 0; |
| 371 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 372 | if (state(hdlc)->settings.dce) { |
| 373 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | pvc->state.active = 0; |
| 375 | } |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | |
Adrian Bunk | 7665a08 | 2005-09-09 23:17:28 -0700 | [diff] [blame] | 382 | static int pvc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Wang Chen | 2baf8a2 | 2008-11-21 16:34:18 -0800 | [diff] [blame] | 384 | pvc_device *pvc = dev->ml_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | fr_proto_pvc_info info; |
| 386 | |
| 387 | if (ifr->ifr_settings.type == IF_GET_PROTO) { |
| 388 | if (dev->type == ARPHRD_ETHER) |
| 389 | ifr->ifr_settings.type = IF_PROTO_FR_ETH_PVC; |
| 390 | else |
| 391 | ifr->ifr_settings.type = IF_PROTO_FR_PVC; |
| 392 | |
| 393 | if (ifr->ifr_settings.size < sizeof(info)) { |
| 394 | /* data size wanted */ |
| 395 | ifr->ifr_settings.size = sizeof(info); |
| 396 | return -ENOBUFS; |
| 397 | } |
| 398 | |
| 399 | info.dlci = pvc->dlci; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 400 | memcpy(info.master, pvc->frad->name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | if (copy_to_user(ifr->ifr_settings.ifs_ifsu.fr_pvc_info, |
| 402 | &info, sizeof(info))) |
| 403 | return -EFAULT; |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
Stephen Hemminger | d71a674 | 2009-08-31 19:50:47 +0000 | [diff] [blame] | 410 | static netdev_tx_t pvc_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Wang Chen | 2baf8a2 | 2008-11-21 16:34:18 -0800 | [diff] [blame] | 412 | pvc_device *pvc = dev->ml_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | if (pvc->state.active) { |
| 415 | if (dev->type == ARPHRD_ETHER) { |
| 416 | int pad = ETH_ZLEN - skb->len; |
| 417 | if (pad > 0) { /* Pad the frame with zeros */ |
| 418 | int len = skb->len; |
| 419 | if (skb_tailroom(skb) < pad) |
| 420 | if (pskb_expand_head(skb, 0, pad, |
| 421 | GFP_ATOMIC)) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 422 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | dev_kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 424 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
| 426 | skb_put(skb, pad); |
| 427 | memset(skb->data + len, 0, pad); |
| 428 | } |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 429 | skb->protocol = cpu_to_be16(ETH_P_802_3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | if (!fr_hard_header(&skb, pvc->dlci)) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 432 | dev->stats.tx_bytes += skb->len; |
| 433 | dev->stats.tx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | if (pvc->state.fecn) /* TX Congestion counter */ |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 435 | dev->stats.tx_compressed++; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 436 | skb->dev = pvc->frad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | dev_queue_xmit(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 438 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 442 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | dev_kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 444 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | static inline void fr_log_dlci_active(pvc_device *pvc) |
| 448 | { |
| 449 | printk(KERN_INFO "%s: DLCI %d [%s%s%s]%s %s\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 450 | pvc->frad->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | pvc->dlci, |
| 452 | pvc->main ? pvc->main->name : "", |
| 453 | pvc->main && pvc->ether ? " " : "", |
| 454 | pvc->ether ? pvc->ether->name : "", |
| 455 | pvc->state.new ? " new" : "", |
| 456 | !pvc->state.exist ? "deleted" : |
| 457 | pvc->state.active ? "active" : "inactive"); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | |
| 462 | static inline u8 fr_lmi_nextseq(u8 x) |
| 463 | { |
| 464 | x++; |
| 465 | return x ? x : 1; |
| 466 | } |
| 467 | |
| 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | static void fr_lmi_send(struct net_device *dev, int fullrep) |
| 470 | { |
| 471 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 472 | struct sk_buff *skb; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 473 | pvc_device *pvc = state(hdlc)->first_pvc; |
| 474 | int lmi = state(hdlc)->settings.lmi; |
| 475 | int dce = state(hdlc)->settings.dce; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 476 | int len = lmi == LMI_ANSI ? LMI_ANSI_LENGTH : LMI_CCITT_CISCO_LENGTH; |
| 477 | int stat_len = (lmi == LMI_CISCO) ? 6 : 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | u8 *data; |
| 479 | int i = 0; |
| 480 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 481 | if (dce && fullrep) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 482 | len += state(hdlc)->dce_pvc_count * (2 + stat_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (len > HDLC_MAX_MRU) { |
| 484 | printk(KERN_WARNING "%s: Too many PVCs while sending " |
| 485 | "LMI full report\n", dev->name); |
| 486 | return; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | skb = dev_alloc_skb(len); |
| 491 | if (!skb) { |
| 492 | printk(KERN_WARNING "%s: Memory squeeze on fr_lmi_send()\n", |
| 493 | dev->name); |
| 494 | return; |
| 495 | } |
| 496 | memset(skb->data, 0, len); |
| 497 | skb_reserve(skb, 4); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 498 | if (lmi == LMI_CISCO) { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 499 | skb->protocol = cpu_to_be16(NLPID_CISCO_LMI); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 500 | fr_hard_header(&skb, LMI_CISCO_DLCI); |
| 501 | } else { |
Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 502 | skb->protocol = cpu_to_be16(NLPID_CCITT_ANSI_LMI); |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 503 | fr_hard_header(&skb, LMI_CCITT_ANSI_DLCI); |
| 504 | } |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 505 | data = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | data[i++] = LMI_CALLREF; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 507 | data[i++] = dce ? LMI_STATUS : LMI_STATUS_ENQUIRY; |
| 508 | if (lmi == LMI_ANSI) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | data[i++] = LMI_ANSI_LOCKSHIFT; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 510 | data[i++] = lmi == LMI_CCITT ? LMI_CCITT_REPTYPE : |
| 511 | LMI_ANSI_CISCO_REPTYPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | data[i++] = LMI_REPT_LEN; |
| 513 | data[i++] = fullrep ? LMI_FULLREP : LMI_INTEGRITY; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 514 | data[i++] = lmi == LMI_CCITT ? LMI_CCITT_ALIVE : LMI_ANSI_CISCO_ALIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | data[i++] = LMI_INTEG_LEN; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 516 | data[i++] = state(hdlc)->txseq = |
| 517 | fr_lmi_nextseq(state(hdlc)->txseq); |
| 518 | data[i++] = state(hdlc)->rxseq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 520 | if (dce && fullrep) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | while (pvc) { |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 522 | data[i++] = lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT : |
| 523 | LMI_ANSI_CISCO_PVCSTAT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | data[i++] = stat_len; |
| 525 | |
| 526 | /* LMI start/restart */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 527 | if (state(hdlc)->reliable && !pvc->state.exist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | pvc->state.exist = pvc->state.new = 1; |
| 529 | fr_log_dlci_active(pvc); |
| 530 | } |
| 531 | |
| 532 | /* ifconfig PVC up */ |
| 533 | if (pvc->open_count && !pvc->state.active && |
| 534 | pvc->state.exist && !pvc->state.new) { |
| 535 | pvc_carrier(1, pvc); |
| 536 | pvc->state.active = 1; |
| 537 | fr_log_dlci_active(pvc); |
| 538 | } |
| 539 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 540 | if (lmi == LMI_CISCO) { |
| 541 | data[i] = pvc->dlci >> 8; |
| 542 | data[i + 1] = pvc->dlci & 0xFF; |
| 543 | } else { |
| 544 | data[i] = (pvc->dlci >> 4) & 0x3F; |
| 545 | data[i + 1] = ((pvc->dlci << 3) & 0x78) | 0x80; |
| 546 | data[i + 2] = 0x80; |
| 547 | } |
| 548 | |
| 549 | if (pvc->state.new) |
| 550 | data[i + 2] |= 0x08; |
| 551 | else if (pvc->state.active) |
| 552 | data[i + 2] |= 0x02; |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | i += stat_len; |
| 555 | pvc = pvc->next; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | skb_put(skb, i); |
| 560 | skb->priority = TC_PRIO_CONTROL; |
| 561 | skb->dev = dev; |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 562 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | dev_queue_xmit(skb); |
| 565 | } |
| 566 | |
| 567 | |
| 568 | |
| 569 | static void fr_set_link_state(int reliable, struct net_device *dev) |
| 570 | { |
| 571 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 572 | pvc_device *pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 574 | state(hdlc)->reliable = reliable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (reliable) { |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 576 | netif_dormant_off(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 577 | state(hdlc)->n391cnt = 0; /* Request full status */ |
| 578 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 580 | if (state(hdlc)->settings.lmi == LMI_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | while (pvc) { /* Activate all PVCs */ |
| 582 | pvc_carrier(1, pvc); |
| 583 | pvc->state.exist = pvc->state.active = 1; |
| 584 | pvc->state.new = 0; |
| 585 | pvc = pvc->next; |
| 586 | } |
| 587 | } |
| 588 | } else { |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 589 | netif_dormant_on(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | while (pvc) { /* Deactivate all PVCs */ |
| 591 | pvc_carrier(0, pvc); |
| 592 | pvc->state.exist = pvc->state.active = 0; |
| 593 | pvc->state.new = 0; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 594 | if (!state(hdlc)->settings.dce) |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 595 | pvc->state.bandwidth = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | pvc = pvc->next; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | static void fr_timer(unsigned long arg) |
| 603 | { |
| 604 | struct net_device *dev = (struct net_device *)arg; |
| 605 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 606 | int i, cnt = 0, reliable; |
| 607 | u32 list; |
| 608 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 609 | if (state(hdlc)->settings.dce) { |
| 610 | reliable = state(hdlc)->request && |
| 611 | time_before(jiffies, state(hdlc)->last_poll + |
| 612 | state(hdlc)->settings.t392 * HZ); |
| 613 | state(hdlc)->request = 0; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 614 | } else { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 615 | state(hdlc)->last_errors <<= 1; /* Shift the list */ |
| 616 | if (state(hdlc)->request) { |
| 617 | if (state(hdlc)->reliable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | printk(KERN_INFO "%s: No LMI status reply " |
| 619 | "received\n", dev->name); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 620 | state(hdlc)->last_errors |= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 623 | list = state(hdlc)->last_errors; |
| 624 | for (i = 0; i < state(hdlc)->settings.n393; i++, list >>= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | cnt += (list & 1); /* errors count */ |
| 626 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 627 | reliable = (cnt < state(hdlc)->settings.n392); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 630 | if (state(hdlc)->reliable != reliable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | printk(KERN_INFO "%s: Link %sreliable\n", dev->name, |
| 632 | reliable ? "" : "un"); |
| 633 | fr_set_link_state(reliable, dev); |
| 634 | } |
| 635 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 636 | if (state(hdlc)->settings.dce) |
| 637 | state(hdlc)->timer.expires = jiffies + |
| 638 | state(hdlc)->settings.t392 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | else { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 640 | if (state(hdlc)->n391cnt) |
| 641 | state(hdlc)->n391cnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 643 | fr_lmi_send(dev, state(hdlc)->n391cnt == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 645 | state(hdlc)->last_poll = jiffies; |
| 646 | state(hdlc)->request = 1; |
| 647 | state(hdlc)->timer.expires = jiffies + |
| 648 | state(hdlc)->settings.t391 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 651 | state(hdlc)->timer.function = fr_timer; |
| 652 | state(hdlc)->timer.data = arg; |
| 653 | add_timer(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | static int fr_lmi_recv(struct net_device *dev, struct sk_buff *skb) |
| 658 | { |
| 659 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | pvc_device *pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | u8 rxseq, txseq; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 662 | int lmi = state(hdlc)->settings.lmi; |
| 663 | int dce = state(hdlc)->settings.dce; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 664 | int stat_len = (lmi == LMI_CISCO) ? 6 : 3, reptype, error, no_ram, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 666 | if (skb->len < (lmi == LMI_ANSI ? LMI_ANSI_LENGTH : |
| 667 | LMI_CCITT_CISCO_LENGTH)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | printk(KERN_INFO "%s: Short LMI frame\n", dev->name); |
| 669 | return 1; |
| 670 | } |
| 671 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 672 | if (skb->data[3] != (lmi == LMI_CISCO ? NLPID_CISCO_LMI : |
| 673 | NLPID_CCITT_ANSI_LMI)) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 674 | printk(KERN_INFO "%s: Received non-LMI frame with LMI DLCI\n", |
| 675 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return 1; |
| 677 | } |
| 678 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 679 | if (skb->data[4] != LMI_CALLREF) { |
| 680 | printk(KERN_INFO "%s: Invalid LMI Call reference (0x%02X)\n", |
| 681 | dev->name, skb->data[4]); |
| 682 | return 1; |
| 683 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 685 | if (skb->data[5] != (dce ? LMI_STATUS_ENQUIRY : LMI_STATUS)) { |
| 686 | printk(KERN_INFO "%s: Invalid LMI Message type (0x%02X)\n", |
| 687 | dev->name, skb->data[5]); |
| 688 | return 1; |
| 689 | } |
| 690 | |
| 691 | if (lmi == LMI_ANSI) { |
| 692 | if (skb->data[6] != LMI_ANSI_LOCKSHIFT) { |
| 693 | printk(KERN_INFO "%s: Not ANSI locking shift in LMI" |
| 694 | " message (0x%02X)\n", dev->name, skb->data[6]); |
| 695 | return 1; |
| 696 | } |
| 697 | i = 7; |
| 698 | } else |
| 699 | i = 6; |
| 700 | |
| 701 | if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_REPTYPE : |
| 702 | LMI_ANSI_CISCO_REPTYPE)) { |
| 703 | printk(KERN_INFO "%s: Not an LMI Report type IE (0x%02X)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | dev->name, skb->data[i]); |
| 705 | return 1; |
| 706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 708 | if (skb->data[++i] != LMI_REPT_LEN) { |
| 709 | printk(KERN_INFO "%s: Invalid LMI Report type IE length" |
| 710 | " (%u)\n", dev->name, skb->data[i]); |
| 711 | return 1; |
| 712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 714 | reptype = skb->data[++i]; |
| 715 | if (reptype != LMI_INTEGRITY && reptype != LMI_FULLREP) { |
| 716 | printk(KERN_INFO "%s: Unsupported LMI Report type (0x%02X)\n", |
| 717 | dev->name, reptype); |
| 718 | return 1; |
| 719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 721 | if (skb->data[++i] != (lmi == LMI_CCITT ? LMI_CCITT_ALIVE : |
| 722 | LMI_ANSI_CISCO_ALIVE)) { |
| 723 | printk(KERN_INFO "%s: Not an LMI Link integrity verification" |
| 724 | " IE (0x%02X)\n", dev->name, skb->data[i]); |
| 725 | return 1; |
| 726 | } |
| 727 | |
| 728 | if (skb->data[++i] != LMI_INTEG_LEN) { |
| 729 | printk(KERN_INFO "%s: Invalid LMI Link integrity verification" |
| 730 | " IE length (%u)\n", dev->name, skb->data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | return 1; |
| 732 | } |
| 733 | i++; |
| 734 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 735 | state(hdlc)->rxseq = skb->data[i++]; /* TX sequence from peer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | rxseq = skb->data[i++]; /* Should confirm our sequence */ |
| 737 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 738 | txseq = state(hdlc)->txseq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 740 | if (dce) |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 741 | state(hdlc)->last_poll = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | error = 0; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 744 | if (!state(hdlc)->reliable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | error = 1; |
| 746 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 747 | if (rxseq == 0 || rxseq != txseq) { /* Ask for full report next time */ |
| 748 | state(hdlc)->n391cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | error = 1; |
| 750 | } |
| 751 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 752 | if (dce) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 753 | if (state(hdlc)->fullrep_sent && !error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | /* Stop sending full report - the last one has been confirmed by DTE */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 755 | state(hdlc)->fullrep_sent = 0; |
| 756 | pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | while (pvc) { |
| 758 | if (pvc->state.new) { |
| 759 | pvc->state.new = 0; |
| 760 | |
| 761 | /* Tell DTE that new PVC is now active */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 762 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | pvc = pvc->next; |
| 765 | } |
| 766 | } |
| 767 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 768 | if (state(hdlc)->dce_changed) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | reptype = LMI_FULLREP; |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 770 | state(hdlc)->fullrep_sent = 1; |
| 771 | state(hdlc)->dce_changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 774 | state(hdlc)->request = 1; /* got request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | fr_lmi_send(dev, reptype == LMI_FULLREP ? 1 : 0); |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | /* DTE */ |
| 780 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 781 | state(hdlc)->request = 0; /* got response, no request pending */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | if (error) |
| 784 | return 0; |
| 785 | |
| 786 | if (reptype != LMI_FULLREP) |
| 787 | return 0; |
| 788 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 789 | pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | while (pvc) { |
| 792 | pvc->state.deleted = 1; |
| 793 | pvc = pvc->next; |
| 794 | } |
| 795 | |
| 796 | no_ram = 0; |
| 797 | while (skb->len >= i + 2 + stat_len) { |
| 798 | u16 dlci; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 799 | u32 bw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | unsigned int active, new; |
| 801 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 802 | if (skb->data[i] != (lmi == LMI_CCITT ? LMI_CCITT_PVCSTAT : |
| 803 | LMI_ANSI_CISCO_PVCSTAT)) { |
| 804 | printk(KERN_INFO "%s: Not an LMI PVC status IE" |
| 805 | " (0x%02X)\n", dev->name, skb->data[i]); |
| 806 | return 1; |
| 807 | } |
| 808 | |
| 809 | if (skb->data[++i] != stat_len) { |
| 810 | printk(KERN_INFO "%s: Invalid LMI PVC status IE length" |
| 811 | " (%u)\n", dev->name, skb->data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return 1; |
| 813 | } |
| 814 | i++; |
| 815 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 816 | new = !! (skb->data[i + 2] & 0x08); |
| 817 | active = !! (skb->data[i + 2] & 0x02); |
| 818 | if (lmi == LMI_CISCO) { |
| 819 | dlci = (skb->data[i] << 8) | skb->data[i + 1]; |
| 820 | bw = (skb->data[i + 3] << 16) | |
| 821 | (skb->data[i + 4] << 8) | |
| 822 | (skb->data[i + 5]); |
| 823 | } else { |
| 824 | dlci = ((skb->data[i] & 0x3F) << 4) | |
| 825 | ((skb->data[i + 1] & 0x78) >> 3); |
| 826 | bw = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | pvc = add_pvc(dev, dlci); |
| 830 | |
| 831 | if (!pvc && !no_ram) { |
| 832 | printk(KERN_WARNING |
| 833 | "%s: Memory squeeze on fr_lmi_recv()\n", |
| 834 | dev->name); |
| 835 | no_ram = 1; |
| 836 | } |
| 837 | |
| 838 | if (pvc) { |
| 839 | pvc->state.exist = 1; |
| 840 | pvc->state.deleted = 0; |
| 841 | if (active != pvc->state.active || |
| 842 | new != pvc->state.new || |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 843 | bw != pvc->state.bandwidth || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | !pvc->state.exist) { |
| 845 | pvc->state.new = new; |
| 846 | pvc->state.active = active; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 847 | pvc->state.bandwidth = bw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | pvc_carrier(active, pvc); |
| 849 | fr_log_dlci_active(pvc); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | i += stat_len; |
| 854 | } |
| 855 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 856 | pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | while (pvc) { |
| 859 | if (pvc->state.deleted && pvc->state.exist) { |
| 860 | pvc_carrier(0, pvc); |
| 861 | pvc->state.active = pvc->state.new = 0; |
| 862 | pvc->state.exist = 0; |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 863 | pvc->state.bandwidth = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | fr_log_dlci_active(pvc); |
| 865 | } |
| 866 | pvc = pvc->next; |
| 867 | } |
| 868 | |
| 869 | /* Next full report after N391 polls */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 870 | state(hdlc)->n391cnt = state(hdlc)->settings.n391; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | static int fr_rx(struct sk_buff *skb) |
| 877 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 878 | struct net_device *frad = skb->dev; |
| 879 | hdlc_device *hdlc = dev_to_hdlc(frad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | fr_hdr *fh = (fr_hdr*)skb->data; |
| 881 | u8 *data = skb->data; |
| 882 | u16 dlci; |
| 883 | pvc_device *pvc; |
| 884 | struct net_device *dev = NULL; |
| 885 | |
| 886 | if (skb->len <= 4 || fh->ea1 || data[2] != FR_UI) |
| 887 | goto rx_error; |
| 888 | |
| 889 | dlci = q922_to_dlci(skb->data); |
| 890 | |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 891 | if ((dlci == LMI_CCITT_ANSI_DLCI && |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 892 | (state(hdlc)->settings.lmi == LMI_ANSI || |
| 893 | state(hdlc)->settings.lmi == LMI_CCITT)) || |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 894 | (dlci == LMI_CISCO_DLCI && |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 895 | state(hdlc)->settings.lmi == LMI_CISCO)) { |
| 896 | if (fr_lmi_recv(frad, skb)) |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 897 | goto rx_error; |
| 898 | dev_kfree_skb_any(skb); |
| 899 | return NET_RX_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | pvc = find_pvc(hdlc, dlci); |
| 903 | if (!pvc) { |
| 904 | #ifdef DEBUG_PKT |
| 905 | printk(KERN_INFO "%s: No PVC for received frame's DLCI %d\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 906 | frad->name, dlci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | #endif |
| 908 | dev_kfree_skb_any(skb); |
| 909 | return NET_RX_DROP; |
| 910 | } |
| 911 | |
| 912 | if (pvc->state.fecn != fh->fecn) { |
| 913 | #ifdef DEBUG_ECN |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 914 | printk(KERN_DEBUG "%s: DLCI %d FECN O%s\n", frad->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | dlci, fh->fecn ? "N" : "FF"); |
| 916 | #endif |
| 917 | pvc->state.fecn ^= 1; |
| 918 | } |
| 919 | |
| 920 | if (pvc->state.becn != fh->becn) { |
| 921 | #ifdef DEBUG_ECN |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 922 | printk(KERN_DEBUG "%s: DLCI %d BECN O%s\n", frad->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | dlci, fh->becn ? "N" : "FF"); |
| 924 | #endif |
| 925 | pvc->state.becn ^= 1; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 930 | frad->stats.rx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | return NET_RX_DROP; |
| 932 | } |
| 933 | |
| 934 | if (data[3] == NLPID_IP) { |
| 935 | skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */ |
| 936 | dev = pvc->main; |
| 937 | skb->protocol = htons(ETH_P_IP); |
| 938 | |
| 939 | } else if (data[3] == NLPID_IPV6) { |
| 940 | skb_pull(skb, 4); /* Remove 4-byte header (hdr, UI, NLPID) */ |
| 941 | dev = pvc->main; |
| 942 | skb->protocol = htons(ETH_P_IPV6); |
| 943 | |
| 944 | } else if (skb->len > 10 && data[3] == FR_PAD && |
| 945 | data[4] == NLPID_SNAP && data[5] == FR_PAD) { |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 946 | u16 oui = ntohs(*(__be16*)(data + 6)); |
| 947 | u16 pid = ntohs(*(__be16*)(data + 8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | skb_pull(skb, 10); |
| 949 | |
| 950 | switch ((((u32)oui) << 16) | pid) { |
| 951 | case ETH_P_ARP: /* routed frame with SNAP */ |
| 952 | case ETH_P_IPX: |
| 953 | case ETH_P_IP: /* a long variant */ |
| 954 | case ETH_P_IPV6: |
| 955 | dev = pvc->main; |
| 956 | skb->protocol = htons(pid); |
| 957 | break; |
| 958 | |
| 959 | case 0x80C20007: /* bridged Ethernet frame */ |
| 960 | if ((dev = pvc->ether) != NULL) |
| 961 | skb->protocol = eth_type_trans(skb, dev); |
| 962 | break; |
| 963 | |
| 964 | default: |
| 965 | printk(KERN_INFO "%s: Unsupported protocol, OUI=%x " |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 966 | "PID=%x\n", frad->name, oui, pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | dev_kfree_skb_any(skb); |
| 968 | return NET_RX_DROP; |
| 969 | } |
| 970 | } else { |
| 971 | printk(KERN_INFO "%s: Unsupported protocol, NLPID=%x " |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 972 | "length = %i\n", frad->name, data[3], skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | dev_kfree_skb_any(skb); |
| 974 | return NET_RX_DROP; |
| 975 | } |
| 976 | |
| 977 | if (dev) { |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 978 | dev->stats.rx_packets++; /* PVC traffic */ |
| 979 | dev->stats.rx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | if (pvc->state.becn) |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 981 | dev->stats.rx_compressed++; |
Krzysztof Halasa | 54364b7 | 2008-06-29 21:48:11 +0200 | [diff] [blame] | 982 | skb->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | netif_rx(skb); |
| 984 | return NET_RX_SUCCESS; |
| 985 | } else { |
| 986 | dev_kfree_skb_any(skb); |
| 987 | return NET_RX_DROP; |
| 988 | } |
| 989 | |
| 990 | rx_error: |
Krzysztof Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 991 | frad->stats.rx_errors++; /* Mark error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | dev_kfree_skb_any(skb); |
| 993 | return NET_RX_DROP; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | |
| 998 | static void fr_start(struct net_device *dev) |
| 999 | { |
| 1000 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 1001 | #ifdef DEBUG_LINK |
| 1002 | printk(KERN_DEBUG "fr_start\n"); |
| 1003 | #endif |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1004 | if (state(hdlc)->settings.lmi != LMI_NONE) { |
| 1005 | state(hdlc)->reliable = 0; |
| 1006 | state(hdlc)->dce_changed = 1; |
| 1007 | state(hdlc)->request = 0; |
| 1008 | state(hdlc)->fullrep_sent = 0; |
| 1009 | state(hdlc)->last_errors = 0xFFFFFFFF; |
| 1010 | state(hdlc)->n391cnt = 0; |
| 1011 | state(hdlc)->txseq = state(hdlc)->rxseq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1013 | init_timer(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | /* First poll after 1 s */ |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1015 | state(hdlc)->timer.expires = jiffies + HZ; |
| 1016 | state(hdlc)->timer.function = fr_timer; |
| 1017 | state(hdlc)->timer.data = (unsigned long)dev; |
| 1018 | add_timer(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } else |
| 1020 | fr_set_link_state(1, dev); |
| 1021 | } |
| 1022 | |
| 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | static void fr_stop(struct net_device *dev) |
| 1025 | { |
| 1026 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 1027 | #ifdef DEBUG_LINK |
| 1028 | printk(KERN_DEBUG "fr_stop\n"); |
| 1029 | #endif |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1030 | if (state(hdlc)->settings.lmi != LMI_NONE) |
| 1031 | del_timer_sync(&state(hdlc)->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | fr_set_link_state(0, dev); |
| 1033 | } |
| 1034 | |
| 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | static void fr_close(struct net_device *dev) |
| 1037 | { |
| 1038 | hdlc_device *hdlc = dev_to_hdlc(dev); |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1039 | pvc_device *pvc = state(hdlc)->first_pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
| 1041 | while (pvc) { /* Shutdown all PVCs for this FRAD */ |
| 1042 | if (pvc->main) |
| 1043 | dev_close(pvc->main); |
| 1044 | if (pvc->ether) |
| 1045 | dev_close(pvc->ether); |
| 1046 | pvc = pvc->next; |
| 1047 | } |
| 1048 | } |
| 1049 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1050 | |
| 1051 | static void pvc_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | { |
| 1053 | dev->type = ARPHRD_DLCI; |
| 1054 | dev->flags = IFF_POINTOPOINT; |
| 1055 | dev->hard_header_len = 10; |
| 1056 | dev->addr_len = 2; |
Eric Dumazet | 93f154b | 2009-05-18 22:19:19 -0700 | [diff] [blame] | 1057 | dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Krzysztof Hałasa | 991990a | 2009-01-08 22:52:11 +0100 | [diff] [blame] | 1060 | static const struct net_device_ops pvc_ops = { |
| 1061 | .ndo_open = pvc_open, |
| 1062 | .ndo_stop = pvc_close, |
| 1063 | .ndo_change_mtu = hdlc_change_mtu, |
| 1064 | .ndo_start_xmit = pvc_xmit, |
| 1065 | .ndo_do_ioctl = pvc_ioctl, |
| 1066 | }; |
| 1067 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1068 | static int fr_add_pvc(struct net_device *frad, unsigned int dlci, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1070 | hdlc_device *hdlc = dev_to_hdlc(frad); |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1071 | pvc_device *pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | struct net_device *dev; |
Jiri Pirko | 1c5cae8 | 2011-04-30 01:21:32 +0000 | [diff] [blame] | 1073 | int used; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1075 | if ((pvc = add_pvc(frad, dlci)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | printk(KERN_WARNING "%s: Memory squeeze on fr_add_pvc()\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1077 | frad->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | return -ENOBUFS; |
| 1079 | } |
| 1080 | |
| 1081 | if (*get_dev_p(pvc, type)) |
| 1082 | return -EEXIST; |
| 1083 | |
| 1084 | used = pvc_is_used(pvc); |
| 1085 | |
Neil Horman | 9cf81e7 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 1086 | if (type == ARPHRD_ETHER) { |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1087 | dev = alloc_netdev(0, "pvceth%d", ether_setup); |
Neil Horman | 9cf81e7 | 2011-07-26 06:05:38 +0000 | [diff] [blame] | 1088 | dev->priv_flags &= ~IFF_TX_SKB_SHARING; |
| 1089 | } else |
Krzysztof Halasa | 141766c | 2008-07-01 14:42:07 +0200 | [diff] [blame] | 1090 | dev = alloc_netdev(0, "pvc%d", pvc_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
| 1092 | if (!dev) { |
| 1093 | printk(KERN_WARNING "%s: Memory squeeze on fr_pvc()\n", |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1094 | frad->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | delete_unused_pvcs(hdlc); |
| 1096 | return -ENOBUFS; |
| 1097 | } |
| 1098 | |
Krzysztof Halasa | 47eaa26 | 2008-02-01 22:39:50 +0100 | [diff] [blame] | 1099 | if (type == ARPHRD_ETHER) |
| 1100 | random_ether_addr(dev->dev_addr); |
| 1101 | else { |
Krzysztof Halasa | abf17ff | 2007-04-27 13:13:33 +0200 | [diff] [blame] | 1102 | *(__be16*)dev->dev_addr = htons(dlci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | dlci_to_q922(dev->broadcast, dlci); |
| 1104 | } |
Krzysztof Hałasa | 991990a | 2009-01-08 22:52:11 +0100 | [diff] [blame] | 1105 | dev->netdev_ops = &pvc_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | dev->mtu = HDLC_MAX_MTU; |
| 1107 | dev->tx_queue_len = 0; |
Wang Chen | 2baf8a2 | 2008-11-21 16:34:18 -0800 | [diff] [blame] | 1108 | dev->ml_priv = pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (register_netdevice(dev) != 0) { |
| 1111 | free_netdev(dev); |
| 1112 | delete_unused_pvcs(hdlc); |
| 1113 | return -EIO; |
| 1114 | } |
| 1115 | |
| 1116 | dev->destructor = free_netdev; |
| 1117 | *get_dev_p(pvc, type) = dev; |
| 1118 | if (!used) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1119 | state(hdlc)->dce_changed = 1; |
| 1120 | state(hdlc)->dce_pvc_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | static int fr_del_pvc(hdlc_device *hdlc, unsigned int dlci, int type) |
| 1128 | { |
| 1129 | pvc_device *pvc; |
| 1130 | struct net_device *dev; |
| 1131 | |
| 1132 | if ((pvc = find_pvc(hdlc, dlci)) == NULL) |
| 1133 | return -ENOENT; |
| 1134 | |
| 1135 | if ((dev = *get_dev_p(pvc, type)) == NULL) |
| 1136 | return -ENOENT; |
| 1137 | |
| 1138 | if (dev->flags & IFF_UP) |
| 1139 | return -EBUSY; /* PVC in use */ |
| 1140 | |
| 1141 | unregister_netdevice(dev); /* the destructor will free_netdev(dev) */ |
| 1142 | *get_dev_p(pvc, type) = NULL; |
| 1143 | |
| 1144 | if (!pvc_is_used(pvc)) { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1145 | state(hdlc)->dce_pvc_count--; |
| 1146 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | delete_unused_pvcs(hdlc); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1154 | static void fr_destroy(struct net_device *frad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | { |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1156 | hdlc_device *hdlc = dev_to_hdlc(frad); |
| 1157 | pvc_device *pvc = state(hdlc)->first_pvc; |
| 1158 | state(hdlc)->first_pvc = NULL; /* All PVCs destroyed */ |
| 1159 | state(hdlc)->dce_pvc_count = 0; |
| 1160 | state(hdlc)->dce_changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
| 1162 | while (pvc) { |
| 1163 | pvc_device *next = pvc->next; |
| 1164 | /* destructors will free_netdev() main and ether */ |
| 1165 | if (pvc->main) |
| 1166 | unregister_netdevice(pvc->main); |
| 1167 | |
| 1168 | if (pvc->ether) |
| 1169 | unregister_netdevice(pvc->ether); |
| 1170 | |
| 1171 | kfree(pvc); |
| 1172 | pvc = next; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1177 | static struct hdlc_proto proto = { |
| 1178 | .close = fr_close, |
| 1179 | .start = fr_start, |
| 1180 | .stop = fr_stop, |
| 1181 | .detach = fr_destroy, |
| 1182 | .ioctl = fr_ioctl, |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 1183 | .netif_rx = fr_rx, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1184 | .module = THIS_MODULE, |
| 1185 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1187 | |
| 1188 | static int fr_ioctl(struct net_device *dev, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | { |
| 1190 | fr_proto __user *fr_s = ifr->ifr_settings.ifs_ifsu.fr; |
| 1191 | const size_t size = sizeof(fr_proto); |
| 1192 | fr_proto new_settings; |
| 1193 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 1194 | fr_proto_pvc pvc; |
| 1195 | int result; |
| 1196 | |
| 1197 | switch (ifr->ifr_settings.type) { |
| 1198 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1199 | if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */ |
| 1200 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | ifr->ifr_settings.type = IF_PROTO_FR; |
| 1202 | if (ifr->ifr_settings.size < size) { |
| 1203 | ifr->ifr_settings.size = size; /* data size wanted */ |
| 1204 | return -ENOBUFS; |
| 1205 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1206 | if (copy_to_user(fr_s, &state(hdlc)->settings, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | return -EFAULT; |
| 1208 | return 0; |
| 1209 | |
| 1210 | case IF_PROTO_FR: |
Rudy Matela | 6f7ad1e | 2009-11-29 23:42:42 -0800 | [diff] [blame] | 1211 | if (!capable(CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | return -EPERM; |
| 1213 | |
Rudy Matela | 6f7ad1e | 2009-11-29 23:42:42 -0800 | [diff] [blame] | 1214 | if (dev->flags & IFF_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | return -EBUSY; |
| 1216 | |
| 1217 | if (copy_from_user(&new_settings, fr_s, size)) |
| 1218 | return -EFAULT; |
| 1219 | |
| 1220 | if (new_settings.lmi == LMI_DEFAULT) |
| 1221 | new_settings.lmi = LMI_ANSI; |
| 1222 | |
| 1223 | if ((new_settings.lmi != LMI_NONE && |
| 1224 | new_settings.lmi != LMI_ANSI && |
Krzysztof Halasa | b3dd65f | 2005-04-21 15:57:25 +0200 | [diff] [blame] | 1225 | new_settings.lmi != LMI_CCITT && |
| 1226 | new_settings.lmi != LMI_CISCO) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | new_settings.t391 < 1 || |
| 1228 | new_settings.t392 < 2 || |
| 1229 | new_settings.n391 < 1 || |
| 1230 | new_settings.n392 < 1 || |
| 1231 | new_settings.n393 < new_settings.n392 || |
| 1232 | new_settings.n393 > 32 || |
| 1233 | (new_settings.dce != 0 && |
| 1234 | new_settings.dce != 1)) |
| 1235 | return -EINVAL; |
| 1236 | |
| 1237 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); |
| 1238 | if (result) |
| 1239 | return result; |
| 1240 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1241 | if (dev_to_hdlc(dev)->proto != &proto) { /* Different proto */ |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 1242 | result = attach_hdlc_protocol(dev, &proto, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1243 | sizeof(struct frad_state)); |
| 1244 | if (result) |
| 1245 | return result; |
| 1246 | state(hdlc)->first_pvc = NULL; |
| 1247 | state(hdlc)->dce_pvc_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1249 | memcpy(&state(hdlc)->settings, &new_settings, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | dev->type = ARPHRD_FRAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | return 0; |
| 1252 | |
| 1253 | case IF_PROTO_FR_ADD_PVC: |
| 1254 | case IF_PROTO_FR_DEL_PVC: |
| 1255 | case IF_PROTO_FR_ADD_ETH_PVC: |
| 1256 | case IF_PROTO_FR_DEL_ETH_PVC: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1257 | if (dev_to_hdlc(dev)->proto != &proto) /* Different proto */ |
| 1258 | return -EINVAL; |
| 1259 | |
Rudy Matela | 6f7ad1e | 2009-11-29 23:42:42 -0800 | [diff] [blame] | 1260 | if (!capable(CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | return -EPERM; |
| 1262 | |
| 1263 | if (copy_from_user(&pvc, ifr->ifr_settings.ifs_ifsu.fr_pvc, |
| 1264 | sizeof(fr_proto_pvc))) |
| 1265 | return -EFAULT; |
| 1266 | |
| 1267 | if (pvc.dlci <= 0 || pvc.dlci >= 1024) |
| 1268 | return -EINVAL; /* Only 10 bits, DLCI 0 reserved */ |
| 1269 | |
| 1270 | if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC || |
| 1271 | ifr->ifr_settings.type == IF_PROTO_FR_DEL_ETH_PVC) |
| 1272 | result = ARPHRD_ETHER; /* bridged Ethernet device */ |
| 1273 | else |
| 1274 | result = ARPHRD_DLCI; |
| 1275 | |
| 1276 | if (ifr->ifr_settings.type == IF_PROTO_FR_ADD_PVC || |
| 1277 | ifr->ifr_settings.type == IF_PROTO_FR_ADD_ETH_PVC) |
| 1278 | return fr_add_pvc(dev, pvc.dlci, result); |
| 1279 | else |
| 1280 | return fr_del_pvc(hdlc, pvc.dlci, result); |
| 1281 | } |
| 1282 | |
| 1283 | return -EINVAL; |
| 1284 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 1285 | |
| 1286 | |
| 1287 | static int __init mod_init(void) |
| 1288 | { |
| 1289 | register_hdlc_protocol(&proto); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | static void __exit mod_exit(void) |
| 1295 | { |
| 1296 | unregister_hdlc_protocol(&proto); |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | module_init(mod_init); |
| 1301 | module_exit(mod_exit); |
| 1302 | |
| 1303 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 1304 | MODULE_DESCRIPTION("Frame-Relay protocol support for generic HDLC"); |
| 1305 | MODULE_LICENSE("GPL v2"); |