Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* net/atm/clip.c - RFC1577 Classical IP over ATM */ |
| 2 | |
| 3 | /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/string.h> |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/kernel.h> /* for UINT_MAX */ |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/netdevice.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/wait.h> |
| 13 | #include <linux/timer.h> |
| 14 | #include <linux/if_arp.h> /* for some manifest constants */ |
| 15 | #include <linux/notifier.h> |
| 16 | #include <linux/atm.h> |
| 17 | #include <linux/atmdev.h> |
| 18 | #include <linux/atmclip.h> |
| 19 | #include <linux/atmarp.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 20 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/ip.h> /* for net/route.h */ |
| 22 | #include <linux/in.h> /* for struct sockaddr_in */ |
| 23 | #include <linux/if.h> /* for IFF_UP */ |
| 24 | #include <linux/inetdevice.h> |
| 25 | #include <linux/bitops.h> |
Randy Dunlap | 4bdbf6c | 2006-07-03 19:47:27 -0700 | [diff] [blame] | 26 | #include <linux/poison.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/proc_fs.h> |
| 28 | #include <linux/seq_file.h> |
| 29 | #include <linux/rcupdate.h> |
| 30 | #include <linux/jhash.h> |
| 31 | #include <net/route.h> /* for struct rtable and routing */ |
| 32 | #include <net/icmp.h> /* icmp_send */ |
| 33 | #include <asm/param.h> /* for HZ */ |
| 34 | #include <asm/byteorder.h> /* for htons etc. */ |
| 35 | #include <asm/system.h> /* save/restore_flags */ |
| 36 | #include <asm/uaccess.h> |
| 37 | #include <asm/atomic.h> |
| 38 | |
| 39 | #include "common.h" |
| 40 | #include "resources.h" |
| 41 | #include "ipcommon.h" |
| 42 | #include <net/atmclip.h> |
| 43 | |
| 44 | |
| 45 | #if 0 |
| 46 | #define DPRINTK(format,args...) printk(format,##args) |
| 47 | #else |
| 48 | #define DPRINTK(format,args...) |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | static struct net_device *clip_devs; |
| 53 | static struct atm_vcc *atmarpd; |
| 54 | static struct neigh_table clip_tbl; |
| 55 | static struct timer_list idle_timer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame^] | 57 | static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | struct sock *sk; |
| 60 | struct atmarp_ctrl *ctrl; |
| 61 | struct sk_buff *skb; |
| 62 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 63 | DPRINTK("to_atmarpd(%d)\n", type); |
| 64 | if (!atmarpd) |
| 65 | return -EUNATCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | skb = alloc_skb(sizeof(struct atmarp_ctrl),GFP_ATOMIC); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 67 | if (!skb) |
| 68 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | ctrl = (struct atmarp_ctrl *) skb_put(skb,sizeof(struct atmarp_ctrl)); |
| 70 | ctrl->type = type; |
| 71 | ctrl->itf_num = itf; |
| 72 | ctrl->ip = ip; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 73 | atm_force_charge(atmarpd, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | sk = sk_atm(atmarpd); |
| 76 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 77 | sk->sk_data_ready(sk, skb->len); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 81 | static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 83 | DPRINTK("link_vcc %p to entry %p (neigh %p)\n", clip_vcc, entry, |
| 84 | entry->neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | clip_vcc->entry = entry; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 86 | clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | clip_vcc->next = entry->vccs; |
| 88 | entry->vccs = clip_vcc; |
| 89 | entry->neigh->used = jiffies; |
| 90 | } |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | static void unlink_clip_vcc(struct clip_vcc *clip_vcc) |
| 93 | { |
| 94 | struct atmarp_entry *entry = clip_vcc->entry; |
| 95 | struct clip_vcc **walk; |
| 96 | |
| 97 | if (!entry) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 98 | printk(KERN_CRIT "!clip_vcc->entry (clip_vcc %p)\n", clip_vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return; |
| 100 | } |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 101 | netif_tx_lock_bh(entry->neigh->dev); /* block clip_start_xmit() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | entry->neigh->used = jiffies; |
| 103 | for (walk = &entry->vccs; *walk; walk = &(*walk)->next) |
| 104 | if (*walk == clip_vcc) { |
| 105 | int error; |
| 106 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 107 | *walk = clip_vcc->next; /* atomic */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | clip_vcc->entry = NULL; |
| 109 | if (clip_vcc->xoff) |
| 110 | netif_wake_queue(entry->neigh->dev); |
| 111 | if (entry->vccs) |
| 112 | goto out; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 113 | entry->expires = jiffies - 1; |
| 114 | /* force resolution or expiration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | error = neigh_update(entry->neigh, NULL, NUD_NONE, |
| 116 | NEIGH_UPDATE_F_ADMIN); |
| 117 | if (error) |
| 118 | printk(KERN_CRIT "unlink_clip_vcc: " |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 119 | "neigh_update failed with %d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | goto out; |
| 121 | } |
| 122 | printk(KERN_CRIT "ATMARP: unlink_clip_vcc failed (entry %p, vcc " |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 123 | "0x%p)\n", entry, clip_vcc); |
| 124 | out: |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 125 | netif_tx_unlock_bh(entry->neigh->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* The neighbour entry n->lock is held. */ |
| 129 | static int neigh_check_cb(struct neighbour *n) |
| 130 | { |
| 131 | struct atmarp_entry *entry = NEIGH2ENTRY(n); |
| 132 | struct clip_vcc *cv; |
| 133 | |
| 134 | for (cv = entry->vccs; cv; cv = cv->next) { |
| 135 | unsigned long exp = cv->last_use + cv->idle_timeout; |
| 136 | |
| 137 | if (cv->idle_timeout && time_after(jiffies, exp)) { |
| 138 | DPRINTK("releasing vcc %p->%p of entry %p\n", |
| 139 | cv, cv->vcc, entry); |
| 140 | vcc_release_async(cv->vcc, -ETIMEDOUT); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (entry->vccs || time_before(jiffies, entry->expires)) |
| 145 | return 0; |
| 146 | |
| 147 | if (atomic_read(&n->refcnt) > 1) { |
| 148 | struct sk_buff *skb; |
| 149 | |
| 150 | DPRINTK("destruction postponed with ref %d\n", |
| 151 | atomic_read(&n->refcnt)); |
| 152 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 153 | while ((skb = skb_dequeue(&n->arp_queue)) != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | dev_kfree_skb(skb); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 159 | DPRINTK("expired neigh %p\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | static void idle_timer_check(unsigned long dummy) |
| 164 | { |
| 165 | write_lock(&clip_tbl.lock); |
| 166 | __neigh_for_each_release(&clip_tbl, neigh_check_cb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 167 | mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | write_unlock(&clip_tbl.lock); |
| 169 | } |
| 170 | |
| 171 | static int clip_arp_rcv(struct sk_buff *skb) |
| 172 | { |
| 173 | struct atm_vcc *vcc; |
| 174 | |
| 175 | DPRINTK("clip_arp_rcv\n"); |
| 176 | vcc = ATM_SKB(skb)->vcc; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 177 | if (!vcc || !atm_charge(vcc, skb->truesize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | dev_kfree_skb_any(skb); |
| 179 | return 0; |
| 180 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 181 | DPRINTK("pushing to %p\n", vcc); |
| 182 | DPRINTK("using %p\n", CLIP_VCC(vcc)->old_push); |
| 183 | CLIP_VCC(vcc)->old_push(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static const unsigned char llc_oui[] = { |
| 188 | 0xaa, /* DSAP: non-ISO */ |
| 189 | 0xaa, /* SSAP: non-ISO */ |
| 190 | 0x03, /* Ctrl: Unnumbered Information Command PDU */ |
| 191 | 0x00, /* OUI: EtherType */ |
| 192 | 0x00, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 193 | 0x00 |
| 194 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 196 | static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | struct clip_vcc *clip_vcc = CLIP_VCC(vcc); |
| 199 | |
| 200 | DPRINTK("clip push\n"); |
| 201 | if (!skb) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 202 | DPRINTK("removing VCC %p\n", clip_vcc); |
| 203 | if (clip_vcc->entry) |
| 204 | unlink_clip_vcc(clip_vcc); |
| 205 | clip_vcc->old_push(vcc, NULL); /* pass on the bad news */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | kfree(clip_vcc); |
| 207 | return; |
| 208 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 209 | atm_return(vcc, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 211 | /* clip_vcc->entry == NULL if we don't have an IP address yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (!skb->dev) { |
| 213 | dev_kfree_skb_any(skb); |
| 214 | return; |
| 215 | } |
| 216 | ATM_SKB(skb)->vcc = vcc; |
| 217 | skb->mac.raw = skb->data; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 218 | if (!clip_vcc->encap |
| 219 | || skb->len < RFC1483LLC_LEN |
| 220 | || memcmp(skb->data, llc_oui, sizeof (llc_oui))) |
| 221 | skb->protocol = htons(ETH_P_IP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | else { |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame^] | 223 | skb->protocol = ((__be16 *) skb->data)[3]; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 224 | skb_pull(skb, RFC1483LLC_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | if (skb->protocol == htons(ETH_P_ARP)) { |
| 226 | PRIV(skb->dev)->stats.rx_packets++; |
| 227 | PRIV(skb->dev)->stats.rx_bytes += skb->len; |
| 228 | clip_arp_rcv(skb); |
| 229 | return; |
| 230 | } |
| 231 | } |
| 232 | clip_vcc->last_use = jiffies; |
| 233 | PRIV(skb->dev)->stats.rx_packets++; |
| 234 | PRIV(skb->dev)->stats.rx_bytes += skb->len; |
| 235 | memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data)); |
| 236 | netif_rx(skb); |
| 237 | } |
| 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /* |
| 240 | * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that |
| 241 | * clip_pop is atomic with respect to the critical section in clip_start_xmit. |
| 242 | */ |
| 243 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 244 | static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
| 246 | struct clip_vcc *clip_vcc = CLIP_VCC(vcc); |
| 247 | struct net_device *dev = skb->dev; |
| 248 | int old; |
| 249 | unsigned long flags; |
| 250 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 251 | DPRINTK("clip_pop(vcc %p)\n", vcc); |
| 252 | clip_vcc->old_pop(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | /* skb->dev == NULL in outbound ARP packets */ |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 254 | if (!dev) |
| 255 | return; |
| 256 | spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags); |
| 257 | if (atm_may_send(vcc, 0)) { |
| 258 | old = xchg(&clip_vcc->xoff, 0); |
| 259 | if (old) |
| 260 | netif_wake_queue(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 262 | spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | static void clip_neigh_destroy(struct neighbour *neigh) |
| 266 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 267 | DPRINTK("clip_neigh_destroy (neigh %p)\n", neigh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (NEIGH2ENTRY(neigh)->vccs) |
| 269 | printk(KERN_CRIT "clip_neigh_destroy: vccs != NULL !!!\n"); |
Randy Dunlap | 4bdbf6c | 2006-07-03 19:47:27 -0700 | [diff] [blame] | 270 | NEIGH2ENTRY(neigh)->vccs = (void *) NEIGHBOR_DEAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 273 | static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 275 | DPRINTK("clip_neigh_solicit (neigh %p, skb %p)\n", neigh, skb); |
| 276 | to_atmarpd(act_need, PRIV(neigh->dev)->number, NEIGH2ENTRY(neigh)->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 279 | static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
| 281 | #ifndef CONFIG_ATM_CLIP_NO_ICMP |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 282 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | #endif |
| 284 | kfree_skb(skb); |
| 285 | } |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | static struct neigh_ops clip_neigh_ops = { |
| 288 | .family = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | .solicit = clip_neigh_solicit, |
| 290 | .error_report = clip_neigh_error, |
| 291 | .output = dev_queue_xmit, |
| 292 | .connected_output = dev_queue_xmit, |
| 293 | .hh_output = dev_queue_xmit, |
| 294 | .queue_xmit = dev_queue_xmit, |
| 295 | }; |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | static int clip_constructor(struct neighbour *neigh) |
| 298 | { |
| 299 | struct atmarp_entry *entry = NEIGH2ENTRY(neigh); |
| 300 | struct net_device *dev = neigh->dev; |
| 301 | struct in_device *in_dev; |
| 302 | struct neigh_parms *parms; |
| 303 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 304 | DPRINTK("clip_constructor (neigh %p, entry %p)\n", neigh, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | neigh->type = inet_addr_type(entry->ip); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 306 | if (neigh->type != RTN_UNICAST) |
| 307 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | rcu_read_lock(); |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 310 | in_dev = __in_dev_get_rcu(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | if (!in_dev) { |
| 312 | rcu_read_unlock(); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | |
| 316 | parms = in_dev->arp_parms; |
| 317 | __neigh_parms_put(neigh->parms); |
| 318 | neigh->parms = neigh_parms_clone(parms); |
| 319 | rcu_read_unlock(); |
| 320 | |
| 321 | neigh->ops = &clip_neigh_ops; |
| 322 | neigh->output = neigh->nud_state & NUD_VALID ? |
| 323 | neigh->ops->connected_output : neigh->ops->output; |
| 324 | entry->neigh = neigh; |
| 325 | entry->vccs = NULL; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 326 | entry->expires = jiffies - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static u32 clip_hash(const void *pkey, const struct net_device *dev) |
| 331 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 332 | return jhash_2words(*(u32 *) pkey, dev->ifindex, clip_tbl.hash_rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static struct neigh_table clip_tbl = { |
| 336 | .family = AF_INET, |
| 337 | .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry), |
| 338 | .key_len = 4, |
| 339 | .hash = clip_hash, |
| 340 | .constructor = clip_constructor, |
| 341 | .id = "clip_arp_cache", |
| 342 | |
| 343 | /* parameters are copied from ARP ... */ |
| 344 | .parms = { |
| 345 | .tbl = &clip_tbl, |
David S. Miller | aa837b5 | 2006-03-20 22:30:23 -0800 | [diff] [blame] | 346 | .neigh_destructor = clip_neigh_destroy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | .base_reachable_time = 30 * HZ, |
| 348 | .retrans_time = 1 * HZ, |
| 349 | .gc_staletime = 60 * HZ, |
| 350 | .reachable_time = 30 * HZ, |
| 351 | .delay_probe_time = 5 * HZ, |
| 352 | .queue_len = 3, |
| 353 | .ucast_probes = 3, |
| 354 | .mcast_probes = 3, |
| 355 | .anycast_delay = 1 * HZ, |
| 356 | .proxy_delay = (8 * HZ) / 10, |
| 357 | .proxy_qlen = 64, |
| 358 | .locktime = 1 * HZ, |
| 359 | }, |
| 360 | .gc_interval = 30 * HZ, |
| 361 | .gc_thresh1 = 128, |
| 362 | .gc_thresh2 = 512, |
| 363 | .gc_thresh3 = 1024, |
| 364 | }; |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */ |
| 367 | |
| 368 | /* |
| 369 | * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means |
| 370 | * to allocate the neighbour entry but not to ask atmarpd for resolution. Also, |
| 371 | * don't increment the usage count. This is used to create entries in |
| 372 | * clip_setentry. |
| 373 | */ |
| 374 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 375 | static int clip_encap(struct atm_vcc *vcc, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | CLIP_VCC(vcc)->encap = mode; |
| 378 | return 0; |
| 379 | } |
| 380 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 381 | static int clip_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
| 383 | struct clip_priv *clip_priv = PRIV(dev); |
| 384 | struct atmarp_entry *entry; |
| 385 | struct atm_vcc *vcc; |
| 386 | int old; |
| 387 | unsigned long flags; |
| 388 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 389 | DPRINTK("clip_start_xmit (skb %p)\n", skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | if (!skb->dst) { |
| 391 | printk(KERN_ERR "clip_start_xmit: skb->dst == NULL\n"); |
| 392 | dev_kfree_skb(skb); |
| 393 | clip_priv->stats.tx_dropped++; |
| 394 | return 0; |
| 395 | } |
| 396 | if (!skb->dst->neighbour) { |
| 397 | #if 0 |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 398 | skb->dst->neighbour = clip_find_neighbour(skb->dst, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (!skb->dst->neighbour) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 400 | dev_kfree_skb(skb); /* lost that one */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | clip_priv->stats.tx_dropped++; |
| 402 | return 0; |
| 403 | } |
| 404 | #endif |
| 405 | printk(KERN_ERR "clip_start_xmit: NO NEIGHBOUR !\n"); |
| 406 | dev_kfree_skb(skb); |
| 407 | clip_priv->stats.tx_dropped++; |
| 408 | return 0; |
| 409 | } |
| 410 | entry = NEIGH2ENTRY(skb->dst->neighbour); |
| 411 | if (!entry->vccs) { |
| 412 | if (time_after(jiffies, entry->expires)) { |
| 413 | /* should be resolved */ |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 414 | entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ; |
| 415 | to_atmarpd(act_need, PRIV(dev)->number, entry->ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS) |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 418 | skb_queue_tail(&entry->neigh->arp_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | else { |
| 420 | dev_kfree_skb(skb); |
| 421 | clip_priv->stats.tx_dropped++; |
| 422 | } |
| 423 | return 0; |
| 424 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 425 | DPRINTK("neigh %p, vccs %p\n", entry, entry->vccs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 427 | DPRINTK("using neighbour %p, vcc %p\n", skb->dst->neighbour, vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | if (entry->vccs->encap) { |
| 429 | void *here; |
| 430 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 431 | here = skb_push(skb, RFC1483LLC_LEN); |
| 432 | memcpy(here, llc_oui, sizeof(llc_oui)); |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame^] | 433 | ((__be16 *) here)[3] = skb->protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc); |
| 436 | ATM_SKB(skb)->atm_options = vcc->atm_options; |
| 437 | entry->vccs->last_use = jiffies; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 438 | DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev); |
| 439 | old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | if (old) { |
| 441 | printk(KERN_WARNING "clip_start_xmit: XOFF->XOFF transition\n"); |
| 442 | return 0; |
| 443 | } |
| 444 | clip_priv->stats.tx_packets++; |
| 445 | clip_priv->stats.tx_bytes += skb->len; |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 446 | vcc->send(vcc, skb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 447 | if (atm_may_send(vcc, 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | entry->vccs->xoff = 0; |
| 449 | return 0; |
| 450 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 451 | spin_lock_irqsave(&clip_priv->xoff_lock, flags); |
| 452 | netif_stop_queue(dev); /* XOFF -> throttle immediately */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | barrier(); |
| 454 | if (!entry->vccs->xoff) |
| 455 | netif_start_queue(dev); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 456 | /* Oh, we just raced with clip_pop. netif_start_queue should be |
| 457 | good enough, because nothing should really be asleep because |
| 458 | of the brief netif_stop_queue. If this isn't true or if it |
| 459 | changes, use netif_wake_queue instead. */ |
| 460 | spin_unlock_irqrestore(&clip_priv->xoff_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | static struct net_device_stats *clip_get_stats(struct net_device *dev) |
| 465 | { |
| 466 | return &PRIV(dev)->stats; |
| 467 | } |
| 468 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 469 | static int clip_mkip(struct atm_vcc *vcc, int timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | { |
| 471 | struct clip_vcc *clip_vcc; |
| 472 | struct sk_buff_head copy; |
| 473 | struct sk_buff *skb; |
| 474 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 475 | if (!vcc->push) |
| 476 | return -EBADFD; |
| 477 | clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL); |
| 478 | if (!clip_vcc) |
| 479 | return -ENOMEM; |
| 480 | DPRINTK("mkip clip_vcc %p vcc %p\n", clip_vcc, vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | clip_vcc->vcc = vcc; |
| 482 | vcc->user_back = clip_vcc; |
| 483 | set_bit(ATM_VF_IS_CLIP, &vcc->flags); |
| 484 | clip_vcc->entry = NULL; |
| 485 | clip_vcc->xoff = 0; |
| 486 | clip_vcc->encap = 1; |
| 487 | clip_vcc->last_use = jiffies; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 488 | clip_vcc->idle_timeout = timeout * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | clip_vcc->old_push = vcc->push; |
| 490 | clip_vcc->old_pop = vcc->pop; |
| 491 | vcc->push = clip_push; |
| 492 | vcc->pop = clip_pop; |
| 493 | skb_queue_head_init(©); |
| 494 | skb_migrate(&sk_atm(vcc)->sk_receive_queue, ©); |
| 495 | /* re-process everything received between connection setup and MKIP */ |
| 496 | while ((skb = skb_dequeue(©)) != NULL) |
| 497 | if (!clip_devs) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 498 | atm_return(vcc, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | kfree_skb(skb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 500 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | unsigned int len = skb->len; |
| 502 | |
YOSHIFUJI Hideaki | fe26109 | 2006-09-18 06:37:58 -0700 | [diff] [blame] | 503 | skb_get(skb); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 504 | clip_push(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | PRIV(skb->dev)->stats.rx_packets--; |
| 506 | PRIV(skb->dev)->stats.rx_bytes -= len; |
YOSHIFUJI Hideaki | fe26109 | 2006-09-18 06:37:58 -0700 | [diff] [blame] | 507 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |
| 509 | return 0; |
| 510 | } |
| 511 | |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame^] | 512 | static int clip_setentry(struct atm_vcc *vcc, __be32 ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | struct neighbour *neigh; |
| 515 | struct atmarp_entry *entry; |
| 516 | int error; |
| 517 | struct clip_vcc *clip_vcc; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 518 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1}} }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | struct rtable *rt; |
| 520 | |
| 521 | if (vcc->push != clip_push) { |
| 522 | printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n"); |
| 523 | return -EBADF; |
| 524 | } |
| 525 | clip_vcc = CLIP_VCC(vcc); |
| 526 | if (!ip) { |
| 527 | if (!clip_vcc->entry) { |
| 528 | printk(KERN_ERR "hiding hidden ATMARP entry\n"); |
| 529 | return 0; |
| 530 | } |
| 531 | DPRINTK("setentry: remove\n"); |
| 532 | unlink_clip_vcc(clip_vcc); |
| 533 | return 0; |
| 534 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 535 | error = ip_route_output_key(&rt, &fl); |
| 536 | if (error) |
| 537 | return error; |
| 538 | neigh = __neigh_lookup(&clip_tbl, &ip, rt->u.dst.dev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | ip_rt_put(rt); |
| 540 | if (!neigh) |
| 541 | return -ENOMEM; |
| 542 | entry = NEIGH2ENTRY(neigh); |
| 543 | if (entry != clip_vcc->entry) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 544 | if (!clip_vcc->entry) |
| 545 | DPRINTK("setentry: add\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | else { |
| 547 | DPRINTK("setentry: update\n"); |
| 548 | unlink_clip_vcc(clip_vcc); |
| 549 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 550 | link_vcc(clip_vcc, entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 552 | error = neigh_update(neigh, llc_oui, NUD_PERMANENT, |
| 553 | NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | neigh_release(neigh); |
| 555 | return error; |
| 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | static void clip_setup(struct net_device *dev) |
| 559 | { |
| 560 | dev->hard_start_xmit = clip_start_xmit; |
| 561 | /* sg_xmit ... */ |
| 562 | dev->get_stats = clip_get_stats; |
| 563 | dev->type = ARPHRD_ATM; |
| 564 | dev->hard_header_len = RFC1483LLC_LEN; |
| 565 | dev->mtu = RFC1626_MTU; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 566 | dev->tx_queue_len = 100; /* "normal" queue (packets) */ |
| 567 | /* When using a "real" qdisc, the qdisc determines the queue */ |
| 568 | /* length. tx_queue_len is only used for the default case, */ |
| 569 | /* without any more elaborate queuing. 100 is a reasonable */ |
| 570 | /* compromise between decent burst-tolerance and protection */ |
| 571 | /* against memory hogs. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | static int clip_create(int number) |
| 575 | { |
| 576 | struct net_device *dev; |
| 577 | struct clip_priv *clip_priv; |
| 578 | int error; |
| 579 | |
| 580 | if (number != -1) { |
| 581 | for (dev = clip_devs; dev; dev = PRIV(dev)->next) |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 582 | if (PRIV(dev)->number == number) |
| 583 | return -EEXIST; |
| 584 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | number = 0; |
| 586 | for (dev = clip_devs; dev; dev = PRIV(dev)->next) |
| 587 | if (PRIV(dev)->number >= number) |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 588 | number = PRIV(dev)->number + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
| 590 | dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup); |
| 591 | if (!dev) |
| 592 | return -ENOMEM; |
| 593 | clip_priv = PRIV(dev); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 594 | sprintf(dev->name, "atm%d", number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | spin_lock_init(&clip_priv->xoff_lock); |
| 596 | clip_priv->number = number; |
| 597 | error = register_netdev(dev); |
| 598 | if (error) { |
| 599 | free_netdev(dev); |
| 600 | return error; |
| 601 | } |
| 602 | clip_priv->next = clip_devs; |
| 603 | clip_devs = dev; |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 604 | DPRINTK("registered (net:%s)\n", dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | return number; |
| 606 | } |
| 607 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 608 | static int clip_device_event(struct notifier_block *this, unsigned long event, |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 609 | void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 611 | struct net_device *dev = arg; |
| 612 | |
| 613 | if (event == NETDEV_UNREGISTER) { |
| 614 | neigh_ifdown(&clip_tbl, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | return NOTIFY_DONE; |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* ignore non-CLIP devices */ |
| 619 | if (dev->type != ARPHRD_ATM || dev->hard_start_xmit != clip_start_xmit) |
| 620 | return NOTIFY_DONE; |
| 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | switch (event) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 623 | case NETDEV_UP: |
| 624 | DPRINTK("clip_device_event NETDEV_UP\n"); |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 625 | to_atmarpd(act_up, PRIV(dev)->number, 0); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 626 | break; |
| 627 | case NETDEV_GOING_DOWN: |
| 628 | DPRINTK("clip_device_event NETDEV_DOWN\n"); |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 629 | to_atmarpd(act_down, PRIV(dev)->number, 0); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 630 | break; |
| 631 | case NETDEV_CHANGE: |
| 632 | case NETDEV_CHANGEMTU: |
| 633 | DPRINTK("clip_device_event NETDEV_CHANGE*\n"); |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 634 | to_atmarpd(act_change, PRIV(dev)->number, 0); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 635 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | return NOTIFY_DONE; |
| 638 | } |
| 639 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 640 | static int clip_inet_event(struct notifier_block *this, unsigned long event, |
| 641 | void *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
| 643 | struct in_device *in_dev; |
| 644 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 645 | in_dev = ((struct in_ifaddr *)ifa)->ifa_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (!in_dev || !in_dev->dev) { |
| 647 | printk(KERN_WARNING "clip_inet_event: no device\n"); |
| 648 | return NOTIFY_DONE; |
| 649 | } |
| 650 | /* |
| 651 | * Transitions are of the down-change-up type, so it's sufficient to |
| 652 | * handle the change on up. |
| 653 | */ |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 654 | if (event != NETDEV_UP) |
| 655 | return NOTIFY_DONE; |
| 656 | return clip_device_event(this, NETDEV_CHANGE, in_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | static struct notifier_block clip_dev_notifier = { |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 661 | .notifier_call = clip_device_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | |
| 665 | |
| 666 | static struct notifier_block clip_inet_notifier = { |
Stephen Hemminger | 5ff765f | 2006-04-14 16:00:59 -0700 | [diff] [blame] | 667 | .notifier_call = clip_inet_event, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | }; |
| 669 | |
| 670 | |
| 671 | |
| 672 | static void atmarpd_close(struct atm_vcc *vcc) |
| 673 | { |
| 674 | DPRINTK("atmarpd_close\n"); |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 675 | |
| 676 | rtnl_lock(); |
| 677 | atmarpd = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | skb_queue_purge(&sk_atm(vcc)->sk_receive_queue); |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 679 | rtnl_unlock(); |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | DPRINTK("(done)\n"); |
| 682 | module_put(THIS_MODULE); |
| 683 | } |
| 684 | |
| 685 | |
| 686 | static struct atmdev_ops atmarpd_dev_ops = { |
| 687 | .close = atmarpd_close |
| 688 | }; |
| 689 | |
| 690 | |
| 691 | static struct atm_dev atmarpd_dev = { |
| 692 | .ops = &atmarpd_dev_ops, |
| 693 | .type = "arpd", |
| 694 | .number = 999, |
| 695 | .lock = SPIN_LOCK_UNLOCKED |
| 696 | }; |
| 697 | |
| 698 | |
| 699 | static int atm_init_atmarp(struct atm_vcc *vcc) |
| 700 | { |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 701 | rtnl_lock(); |
| 702 | if (atmarpd) { |
| 703 | rtnl_unlock(); |
| 704 | return -EADDRINUSE; |
| 705 | } |
| 706 | |
Stephen Hemminger | 2d90739 | 2006-04-14 15:56:02 -0700 | [diff] [blame] | 707 | mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ); |
| 708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | atmarpd = vcc; |
| 710 | set_bit(ATM_VF_META,&vcc->flags); |
| 711 | set_bit(ATM_VF_READY,&vcc->flags); |
| 712 | /* allow replies and avoid getting closed if signaling dies */ |
| 713 | vcc->dev = &atmarpd_dev; |
| 714 | vcc_insert_socket(sk_atm(vcc)); |
| 715 | vcc->push = NULL; |
| 716 | vcc->pop = NULL; /* crash */ |
| 717 | vcc->push_oam = NULL; /* crash */ |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 718 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
| 723 | { |
| 724 | struct atm_vcc *vcc = ATM_SD(sock); |
| 725 | int err = 0; |
| 726 | |
| 727 | switch (cmd) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 728 | case SIOCMKCLIP: |
| 729 | case ATMARPD_CTRL: |
| 730 | case ATMARP_MKIP: |
| 731 | case ATMARP_SETENTRY: |
| 732 | case ATMARP_ENCAP: |
| 733 | if (!capable(CAP_NET_ADMIN)) |
| 734 | return -EPERM; |
| 735 | break; |
| 736 | default: |
| 737 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | switch (cmd) { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 741 | case SIOCMKCLIP: |
| 742 | err = clip_create(arg); |
| 743 | break; |
| 744 | case ATMARPD_CTRL: |
| 745 | err = atm_init_atmarp(vcc); |
| 746 | if (!err) { |
| 747 | sock->state = SS_CONNECTED; |
| 748 | __module_get(THIS_MODULE); |
| 749 | } |
| 750 | break; |
| 751 | case ATMARP_MKIP: |
| 752 | err = clip_mkip(vcc, arg); |
| 753 | break; |
| 754 | case ATMARP_SETENTRY: |
Al Viro | 30d492d | 2006-11-14 21:11:29 -0800 | [diff] [blame^] | 755 | err = clip_setentry(vcc, (__force __be32)arg); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 756 | break; |
| 757 | case ATMARP_ENCAP: |
| 758 | err = clip_encap(vcc, arg); |
| 759 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | return err; |
| 762 | } |
| 763 | |
| 764 | static struct atm_ioctl clip_ioctl_ops = { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 765 | .owner = THIS_MODULE, |
| 766 | .ioctl = clip_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | #ifdef CONFIG_PROC_FS |
| 770 | |
| 771 | static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr) |
| 772 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 773 | static int code[] = { 1, 2, 10, 6, 1, 0 }; |
| 774 | static int e164[] = { 1, 8, 4, 6, 1, 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | if (*addr->sas_addr.pub) { |
| 777 | seq_printf(seq, "%s", addr->sas_addr.pub); |
| 778 | if (*addr->sas_addr.prv) |
| 779 | seq_putc(seq, '+'); |
| 780 | } else if (!*addr->sas_addr.prv) { |
| 781 | seq_printf(seq, "%s", "(none)"); |
| 782 | return; |
| 783 | } |
| 784 | if (*addr->sas_addr.prv) { |
| 785 | unsigned char *prv = addr->sas_addr.prv; |
| 786 | int *fields; |
| 787 | int i, j; |
| 788 | |
| 789 | fields = *prv == ATM_AFI_E164 ? e164 : code; |
| 790 | for (i = 0; fields[i]; i++) { |
| 791 | for (j = fields[i]; j; j--) |
| 792 | seq_printf(seq, "%02X", *prv++); |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 793 | if (fields[i + 1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | seq_putc(seq, '.'); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | /* This means the neighbour entry has no attached VCC objects. */ |
| 800 | #define SEQ_NO_VCC_TOKEN ((void *) 2) |
| 801 | |
| 802 | static void atmarp_info(struct seq_file *seq, struct net_device *dev, |
| 803 | struct atmarp_entry *entry, struct clip_vcc *clip_vcc) |
| 804 | { |
| 805 | unsigned long exp; |
| 806 | char buf[17]; |
| 807 | int svc, llc, off; |
| 808 | |
| 809 | svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || |
| 810 | (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC)); |
| 811 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 812 | llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
| 814 | if (clip_vcc == SEQ_NO_VCC_TOKEN) |
| 815 | exp = entry->neigh->used; |
| 816 | else |
| 817 | exp = clip_vcc->last_use; |
| 818 | |
| 819 | exp = (jiffies - exp) / HZ; |
| 820 | |
| 821 | seq_printf(seq, "%-6s%-4s%-4s%5ld ", |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 822 | dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
| 824 | off = scnprintf(buf, sizeof(buf) - 1, "%d.%d.%d.%d", |
| 825 | NIPQUAD(entry->ip)); |
| 826 | while (off < 16) |
| 827 | buf[off++] = ' '; |
| 828 | buf[off] = '\0'; |
| 829 | seq_printf(seq, "%s", buf); |
| 830 | |
| 831 | if (clip_vcc == SEQ_NO_VCC_TOKEN) { |
| 832 | if (time_before(jiffies, entry->expires)) |
| 833 | seq_printf(seq, "(resolving)\n"); |
| 834 | else |
| 835 | seq_printf(seq, "(expired, ref %d)\n", |
| 836 | atomic_read(&entry->neigh->refcnt)); |
| 837 | } else if (!svc) { |
| 838 | seq_printf(seq, "%d.%d.%d\n", |
| 839 | clip_vcc->vcc->dev->number, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 840 | clip_vcc->vcc->vpi, clip_vcc->vcc->vci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } else { |
| 842 | svc_addr(seq, &clip_vcc->vcc->remote); |
| 843 | seq_putc(seq, '\n'); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | struct clip_seq_state { |
| 848 | /* This member must be first. */ |
| 849 | struct neigh_seq_state ns; |
| 850 | |
| 851 | /* Local to clip specific iteration. */ |
| 852 | struct clip_vcc *vcc; |
| 853 | }; |
| 854 | |
| 855 | static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e, |
| 856 | struct clip_vcc *curr) |
| 857 | { |
| 858 | if (!curr) { |
| 859 | curr = e->vccs; |
| 860 | if (!curr) |
| 861 | return SEQ_NO_VCC_TOKEN; |
| 862 | return curr; |
| 863 | } |
| 864 | if (curr == SEQ_NO_VCC_TOKEN) |
| 865 | return NULL; |
| 866 | |
| 867 | curr = curr->next; |
| 868 | |
| 869 | return curr; |
| 870 | } |
| 871 | |
| 872 | static void *clip_seq_vcc_walk(struct clip_seq_state *state, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 873 | struct atmarp_entry *e, loff_t * pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | { |
| 875 | struct clip_vcc *vcc = state->vcc; |
| 876 | |
| 877 | vcc = clip_seq_next_vcc(e, vcc); |
| 878 | if (vcc && pos != NULL) { |
| 879 | while (*pos) { |
| 880 | vcc = clip_seq_next_vcc(e, vcc); |
| 881 | if (!vcc) |
| 882 | break; |
| 883 | --(*pos); |
| 884 | } |
| 885 | } |
| 886 | state->vcc = vcc; |
| 887 | |
| 888 | return vcc; |
| 889 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | static void *clip_seq_sub_iter(struct neigh_seq_state *_state, |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 892 | struct neighbour *n, loff_t * pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 894 | struct clip_seq_state *state = (struct clip_seq_state *)_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos); |
| 897 | } |
| 898 | |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 899 | static void *clip_seq_start(struct seq_file *seq, loff_t * pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
| 901 | return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY); |
| 902 | } |
| 903 | |
| 904 | static int clip_seq_show(struct seq_file *seq, void *v) |
| 905 | { |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 906 | static char atm_arp_banner[] = |
| 907 | "IPitf TypeEncp Idle IP address ATM address\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
| 909 | if (v == SEQ_START_TOKEN) { |
| 910 | seq_puts(seq, atm_arp_banner); |
| 911 | } else { |
| 912 | struct clip_seq_state *state = seq->private; |
| 913 | struct neighbour *n = v; |
| 914 | struct clip_vcc *vcc = state->vcc; |
| 915 | |
| 916 | atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc); |
| 917 | } |
Stephen Hemminger | e49e76d | 2006-04-14 15:59:37 -0700 | [diff] [blame] | 918 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | static struct seq_operations arp_seq_ops = { |
| 922 | .start = clip_seq_start, |
| 923 | .next = neigh_seq_next, |
| 924 | .stop = neigh_seq_stop, |
| 925 | .show = clip_seq_show, |
| 926 | }; |
| 927 | |
| 928 | static int arp_seq_open(struct inode *inode, struct file *file) |
| 929 | { |
| 930 | struct clip_seq_state *state; |
| 931 | struct seq_file *seq; |
| 932 | int rc = -EAGAIN; |
| 933 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 934 | state = kzalloc(sizeof(*state), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | if (!state) { |
| 936 | rc = -ENOMEM; |
| 937 | goto out_kfree; |
| 938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | state->ns.neigh_sub_iter = clip_seq_sub_iter; |
| 940 | |
| 941 | rc = seq_open(file, &arp_seq_ops); |
| 942 | if (rc) |
| 943 | goto out_kfree; |
| 944 | |
| 945 | seq = file->private_data; |
| 946 | seq->private = state; |
| 947 | out: |
| 948 | return rc; |
| 949 | |
| 950 | out_kfree: |
| 951 | kfree(state); |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | static struct file_operations arp_seq_fops = { |
| 956 | .open = arp_seq_open, |
| 957 | .read = seq_read, |
| 958 | .llseek = seq_lseek, |
| 959 | .release = seq_release_private, |
| 960 | .owner = THIS_MODULE |
| 961 | }; |
| 962 | #endif |
| 963 | |
| 964 | static int __init atm_clip_init(void) |
| 965 | { |
Simon Kelley | bd89efc | 2006-05-12 14:56:08 -0700 | [diff] [blame] | 966 | neigh_table_init_no_netlink(&clip_tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | clip_tbl_hook = &clip_tbl; |
| 969 | register_atm_ioctl(&clip_ioctl_ops); |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 970 | register_netdevice_notifier(&clip_dev_notifier); |
| 971 | register_inetaddr_notifier(&clip_inet_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
Stephen Hemminger | 2d90739 | 2006-04-14 15:56:02 -0700 | [diff] [blame] | 973 | setup_timer(&idle_timer, idle_timer_check, 0); |
| 974 | |
Adrian Bunk | 2478173 | 2006-07-09 12:13:18 -0700 | [diff] [blame] | 975 | #ifdef CONFIG_PROC_FS |
| 976 | { |
| 977 | struct proc_dir_entry *p; |
| 978 | |
| 979 | p = create_proc_entry("arp", S_IRUGO, atm_proc_root); |
| 980 | if (p) |
| 981 | p->proc_fops = &arp_seq_fops; |
| 982 | } |
| 983 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static void __exit atm_clip_exit(void) |
| 989 | { |
| 990 | struct net_device *dev, *next; |
| 991 | |
| 992 | remove_proc_entry("arp", atm_proc_root); |
| 993 | |
Stephen Hemminger | f3a0592 | 2006-04-14 15:07:27 -0700 | [diff] [blame] | 994 | unregister_inetaddr_notifier(&clip_inet_notifier); |
| 995 | unregister_netdevice_notifier(&clip_dev_notifier); |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | deregister_atm_ioctl(&clip_ioctl_ops); |
| 998 | |
| 999 | /* First, stop the idle timer, so it stops banging |
| 1000 | * on the table. |
| 1001 | */ |
Stephen Hemminger | 2d90739 | 2006-04-14 15:56:02 -0700 | [diff] [blame] | 1002 | del_timer_sync(&idle_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
| 1004 | /* Next, purge the table, so that the device |
| 1005 | * unregister loop below does not hang due to |
| 1006 | * device references remaining in the table. |
| 1007 | */ |
| 1008 | neigh_ifdown(&clip_tbl, NULL); |
| 1009 | |
| 1010 | dev = clip_devs; |
| 1011 | while (dev) { |
| 1012 | next = PRIV(dev)->next; |
| 1013 | unregister_netdev(dev); |
| 1014 | free_netdev(dev); |
| 1015 | dev = next; |
| 1016 | } |
| 1017 | |
| 1018 | /* Now it is safe to fully shutdown whole table. */ |
| 1019 | neigh_table_clear(&clip_tbl); |
| 1020 | |
| 1021 | clip_tbl_hook = NULL; |
| 1022 | } |
| 1023 | |
| 1024 | module_init(atm_clip_init); |
| 1025 | module_exit(atm_clip_exit); |
Stephen Hemminger | 4909e488 | 2006-04-14 16:01:26 -0700 | [diff] [blame] | 1026 | MODULE_AUTHOR("Werner Almesberger"); |
| 1027 | MODULE_DESCRIPTION("Classical/IP over ATM interface"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | MODULE_LICENSE("GPL"); |