11da177e4SLinus Torvalds /* net/atm/clip.c - RFC1577 Classical IP over ATM */ 21da177e4SLinus Torvalds 31da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ 41da177e4SLinus Torvalds 599824461SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ 699824461SJoe Perches 71da177e4SLinus Torvalds #include <linux/string.h> 81da177e4SLinus Torvalds #include <linux/errno.h> 91da177e4SLinus Torvalds #include <linux/kernel.h> /* for UINT_MAX */ 101da177e4SLinus Torvalds #include <linux/module.h> 111da177e4SLinus Torvalds #include <linux/init.h> 121da177e4SLinus Torvalds #include <linux/netdevice.h> 131da177e4SLinus Torvalds #include <linux/skbuff.h> 141da177e4SLinus Torvalds #include <linux/wait.h> 151da177e4SLinus Torvalds #include <linux/timer.h> 161da177e4SLinus Torvalds #include <linux/if_arp.h> /* for some manifest constants */ 171da177e4SLinus Torvalds #include <linux/notifier.h> 181da177e4SLinus Torvalds #include <linux/atm.h> 191da177e4SLinus Torvalds #include <linux/atmdev.h> 201da177e4SLinus Torvalds #include <linux/atmclip.h> 211da177e4SLinus Torvalds #include <linux/atmarp.h> 224fc268d2SRandy Dunlap #include <linux/capability.h> 231da177e4SLinus Torvalds #include <linux/ip.h> /* for net/route.h */ 241da177e4SLinus Torvalds #include <linux/in.h> /* for struct sockaddr_in */ 251da177e4SLinus Torvalds #include <linux/if.h> /* for IFF_UP */ 261da177e4SLinus Torvalds #include <linux/inetdevice.h> 271da177e4SLinus Torvalds #include <linux/bitops.h> 284bdbf6c0SRandy Dunlap #include <linux/poison.h> 291da177e4SLinus Torvalds #include <linux/proc_fs.h> 301da177e4SLinus Torvalds #include <linux/seq_file.h> 311da177e4SLinus Torvalds #include <linux/rcupdate.h> 321da177e4SLinus Torvalds #include <linux/jhash.h> 331da177e4SLinus Torvalds #include <net/route.h> /* for struct rtable and routing */ 341da177e4SLinus Torvalds #include <net/icmp.h> /* icmp_send */ 35*e956ea1bSJoe Perches #include <linux/param.h> /* for HZ */ 36*e956ea1bSJoe Perches #include <linux/uaccess.h> 371da177e4SLinus Torvalds #include <asm/byteorder.h> /* for htons etc. */ 381da177e4SLinus Torvalds #include <asm/system.h> /* save/restore_flags */ 391da177e4SLinus Torvalds #include <asm/atomic.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include "common.h" 421da177e4SLinus Torvalds #include "resources.h" 431da177e4SLinus Torvalds #include <net/atmclip.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static struct net_device *clip_devs; 461da177e4SLinus Torvalds static struct atm_vcc *atmarpd; 471da177e4SLinus Torvalds static struct neigh_table clip_tbl; 481da177e4SLinus Torvalds static struct timer_list idle_timer; 491da177e4SLinus Torvalds 5030d492daSAl Viro static int to_atmarpd(enum atmarp_ctrl_type type, int itf, __be32 ip) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds struct sock *sk; 531da177e4SLinus Torvalds struct atmarp_ctrl *ctrl; 541da177e4SLinus Torvalds struct sk_buff *skb; 551da177e4SLinus Torvalds 5699824461SJoe Perches pr_debug("(%d)\n", type); 57e49e76dbSStephen Hemminger if (!atmarpd) 58e49e76dbSStephen Hemminger return -EUNATCH; 591da177e4SLinus Torvalds skb = alloc_skb(sizeof(struct atmarp_ctrl), GFP_ATOMIC); 60e49e76dbSStephen Hemminger if (!skb) 61e49e76dbSStephen Hemminger return -ENOMEM; 621da177e4SLinus Torvalds ctrl = (struct atmarp_ctrl *)skb_put(skb, sizeof(struct atmarp_ctrl)); 631da177e4SLinus Torvalds ctrl->type = type; 641da177e4SLinus Torvalds ctrl->itf_num = itf; 651da177e4SLinus Torvalds ctrl->ip = ip; 661da177e4SLinus Torvalds atm_force_charge(atmarpd, skb->truesize); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds sk = sk_atm(atmarpd); 691da177e4SLinus Torvalds skb_queue_tail(&sk->sk_receive_queue, skb); 701da177e4SLinus Torvalds sk->sk_data_ready(sk, skb->len); 711da177e4SLinus Torvalds return 0; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds static void link_vcc(struct clip_vcc *clip_vcc, struct atmarp_entry *entry) 751da177e4SLinus Torvalds { 7699824461SJoe Perches pr_debug("%p to entry %p (neigh %p)\n", clip_vcc, entry, entry->neigh); 771da177e4SLinus Torvalds clip_vcc->entry = entry; 781da177e4SLinus Torvalds clip_vcc->xoff = 0; /* @@@ may overrun buffer by one packet */ 791da177e4SLinus Torvalds clip_vcc->next = entry->vccs; 801da177e4SLinus Torvalds entry->vccs = clip_vcc; 811da177e4SLinus Torvalds entry->neigh->used = jiffies; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds static void unlink_clip_vcc(struct clip_vcc *clip_vcc) 851da177e4SLinus Torvalds { 861da177e4SLinus Torvalds struct atmarp_entry *entry = clip_vcc->entry; 871da177e4SLinus Torvalds struct clip_vcc **walk; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds if (!entry) { 9099824461SJoe Perches pr_crit("!clip_vcc->entry (clip_vcc %p)\n", clip_vcc); 911da177e4SLinus Torvalds return; 921da177e4SLinus Torvalds } 93932ff279SHerbert Xu netif_tx_lock_bh(entry->neigh->dev); /* block clip_start_xmit() */ 941da177e4SLinus Torvalds entry->neigh->used = jiffies; 951da177e4SLinus Torvalds for (walk = &entry->vccs; *walk; walk = &(*walk)->next) 961da177e4SLinus Torvalds if (*walk == clip_vcc) { 971da177e4SLinus Torvalds int error; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds *walk = clip_vcc->next; /* atomic */ 1001da177e4SLinus Torvalds clip_vcc->entry = NULL; 1011da177e4SLinus Torvalds if (clip_vcc->xoff) 1021da177e4SLinus Torvalds netif_wake_queue(entry->neigh->dev); 1031da177e4SLinus Torvalds if (entry->vccs) 1041da177e4SLinus Torvalds goto out; 1051da177e4SLinus Torvalds entry->expires = jiffies - 1; 1061da177e4SLinus Torvalds /* force resolution or expiration */ 1071da177e4SLinus Torvalds error = neigh_update(entry->neigh, NULL, NUD_NONE, 1081da177e4SLinus Torvalds NEIGH_UPDATE_F_ADMIN); 1091da177e4SLinus Torvalds if (error) 11099824461SJoe Perches pr_crit("neigh_update failed with %d\n", error); 1111da177e4SLinus Torvalds goto out; 1121da177e4SLinus Torvalds } 11399824461SJoe Perches pr_crit("ATMARP: failed (entry %p, vcc 0x%p)\n", entry, clip_vcc); 1141da177e4SLinus Torvalds out: 115932ff279SHerbert Xu netif_tx_unlock_bh(entry->neigh->dev); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* The neighbour entry n->lock is held. */ 1191da177e4SLinus Torvalds static int neigh_check_cb(struct neighbour *n) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds struct atmarp_entry *entry = NEIGH2ENTRY(n); 1221da177e4SLinus Torvalds struct clip_vcc *cv; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds for (cv = entry->vccs; cv; cv = cv->next) { 1251da177e4SLinus Torvalds unsigned long exp = cv->last_use + cv->idle_timeout; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds if (cv->idle_timeout && time_after(jiffies, exp)) { 12852240062SStephen Hemminger pr_debug("releasing vcc %p->%p of entry %p\n", 1291da177e4SLinus Torvalds cv, cv->vcc, entry); 1301da177e4SLinus Torvalds vcc_release_async(cv->vcc, -ETIMEDOUT); 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds if (entry->vccs || time_before(jiffies, entry->expires)) 1351da177e4SLinus Torvalds return 0; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds if (atomic_read(&n->refcnt) > 1) { 1381da177e4SLinus Torvalds struct sk_buff *skb; 1391da177e4SLinus Torvalds 14052240062SStephen Hemminger pr_debug("destruction postponed with ref %d\n", 1411da177e4SLinus Torvalds atomic_read(&n->refcnt)); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds while ((skb = skb_dequeue(&n->arp_queue)) != NULL) 1441da177e4SLinus Torvalds dev_kfree_skb(skb); 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds return 0; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 14952240062SStephen Hemminger pr_debug("expired neigh %p\n", n); 1501da177e4SLinus Torvalds return 1; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static void idle_timer_check(unsigned long dummy) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds write_lock(&clip_tbl.lock); 1561da177e4SLinus Torvalds __neigh_for_each_release(&clip_tbl, neigh_check_cb); 1571da177e4SLinus Torvalds mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ); 1581da177e4SLinus Torvalds write_unlock(&clip_tbl.lock); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static int clip_arp_rcv(struct sk_buff *skb) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds struct atm_vcc *vcc; 1641da177e4SLinus Torvalds 16599824461SJoe Perches pr_debug("\n"); 1661da177e4SLinus Torvalds vcc = ATM_SKB(skb)->vcc; 1671da177e4SLinus Torvalds if (!vcc || !atm_charge(vcc, skb->truesize)) { 1681da177e4SLinus Torvalds dev_kfree_skb_any(skb); 1691da177e4SLinus Torvalds return 0; 1701da177e4SLinus Torvalds } 17152240062SStephen Hemminger pr_debug("pushing to %p\n", vcc); 17252240062SStephen Hemminger pr_debug("using %p\n", CLIP_VCC(vcc)->old_push); 1731da177e4SLinus Torvalds CLIP_VCC(vcc)->old_push(vcc, skb); 1741da177e4SLinus Torvalds return 0; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds static const unsigned char llc_oui[] = { 1781da177e4SLinus Torvalds 0xaa, /* DSAP: non-ISO */ 1791da177e4SLinus Torvalds 0xaa, /* SSAP: non-ISO */ 1801da177e4SLinus Torvalds 0x03, /* Ctrl: Unnumbered Information Command PDU */ 1811da177e4SLinus Torvalds 0x00, /* OUI: EtherType */ 1821da177e4SLinus Torvalds 0x00, 183e49e76dbSStephen Hemminger 0x00 184e49e76dbSStephen Hemminger }; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds static void clip_push(struct atm_vcc *vcc, struct sk_buff *skb) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds struct clip_vcc *clip_vcc = CLIP_VCC(vcc); 1891da177e4SLinus Torvalds 19099824461SJoe Perches pr_debug("\n"); 1911da177e4SLinus Torvalds if (!skb) { 19252240062SStephen Hemminger pr_debug("removing VCC %p\n", clip_vcc); 193e49e76dbSStephen Hemminger if (clip_vcc->entry) 194e49e76dbSStephen Hemminger unlink_clip_vcc(clip_vcc); 1951da177e4SLinus Torvalds clip_vcc->old_push(vcc, NULL); /* pass on the bad news */ 1961da177e4SLinus Torvalds kfree(clip_vcc); 1971da177e4SLinus Torvalds return; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds atm_return(vcc, skb->truesize); 2001da177e4SLinus Torvalds skb->dev = clip_vcc->entry ? clip_vcc->entry->neigh->dev : clip_devs; 2011da177e4SLinus Torvalds /* clip_vcc->entry == NULL if we don't have an IP address yet */ 2021da177e4SLinus Torvalds if (!skb->dev) { 2031da177e4SLinus Torvalds dev_kfree_skb_any(skb); 2041da177e4SLinus Torvalds return; 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds ATM_SKB(skb)->vcc = vcc; 207459a98edSArnaldo Carvalho de Melo skb_reset_mac_header(skb); 208*e956ea1bSJoe Perches if (!clip_vcc->encap || 209*e956ea1bSJoe Perches skb->len < RFC1483LLC_LEN || 210*e956ea1bSJoe Perches memcmp(skb->data, llc_oui, sizeof(llc_oui))) 211e49e76dbSStephen Hemminger skb->protocol = htons(ETH_P_IP); 2121da177e4SLinus Torvalds else { 21330d492daSAl Viro skb->protocol = ((__be16 *)skb->data)[3]; 2141da177e4SLinus Torvalds skb_pull(skb, RFC1483LLC_LEN); 2151da177e4SLinus Torvalds if (skb->protocol == htons(ETH_P_ARP)) { 2161a6afe8aSStephen Hemminger skb->dev->stats.rx_packets++; 2171a6afe8aSStephen Hemminger skb->dev->stats.rx_bytes += skb->len; 2181da177e4SLinus Torvalds clip_arp_rcv(skb); 2191da177e4SLinus Torvalds return; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds clip_vcc->last_use = jiffies; 2231a6afe8aSStephen Hemminger skb->dev->stats.rx_packets++; 2241a6afe8aSStephen Hemminger skb->dev->stats.rx_bytes += skb->len; 2251da177e4SLinus Torvalds memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data)); 2261da177e4SLinus Torvalds netif_rx(skb); 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds * Note: these spinlocks _must_not_ block on non-SMP. The only goal is that 2311da177e4SLinus Torvalds * clip_pop is atomic with respect to the critical section in clip_start_xmit. 2321da177e4SLinus Torvalds */ 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds static void clip_pop(struct atm_vcc *vcc, struct sk_buff *skb) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds struct clip_vcc *clip_vcc = CLIP_VCC(vcc); 2371da177e4SLinus Torvalds struct net_device *dev = skb->dev; 2381da177e4SLinus Torvalds int old; 2391da177e4SLinus Torvalds unsigned long flags; 2401da177e4SLinus Torvalds 24199824461SJoe Perches pr_debug("(vcc %p)\n", vcc); 2421da177e4SLinus Torvalds clip_vcc->old_pop(vcc, skb); 2431da177e4SLinus Torvalds /* skb->dev == NULL in outbound ARP packets */ 244e49e76dbSStephen Hemminger if (!dev) 245e49e76dbSStephen Hemminger return; 2461da177e4SLinus Torvalds spin_lock_irqsave(&PRIV(dev)->xoff_lock, flags); 2471da177e4SLinus Torvalds if (atm_may_send(vcc, 0)) { 2481da177e4SLinus Torvalds old = xchg(&clip_vcc->xoff, 0); 249e49e76dbSStephen Hemminger if (old) 250e49e76dbSStephen Hemminger netif_wake_queue(dev); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds spin_unlock_irqrestore(&PRIV(dev)->xoff_lock, flags); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static void clip_neigh_solicit(struct neighbour *neigh, struct sk_buff *skb) 2561da177e4SLinus Torvalds { 25799824461SJoe Perches pr_debug("(neigh %p, skb %p)\n", neigh, skb); 2581da177e4SLinus Torvalds to_atmarpd(act_need, PRIV(neigh->dev)->number, NEIGH2ENTRY(neigh)->ip); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds static void clip_neigh_error(struct neighbour *neigh, struct sk_buff *skb) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds #ifndef CONFIG_ATM_CLIP_NO_ICMP 2641da177e4SLinus Torvalds icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 2651da177e4SLinus Torvalds #endif 2661da177e4SLinus Torvalds kfree_skb(skb); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 26989d69d2bSStephen Hemminger static const struct neigh_ops clip_neigh_ops = { 2701da177e4SLinus Torvalds .family = AF_INET, 2711da177e4SLinus Torvalds .solicit = clip_neigh_solicit, 2721da177e4SLinus Torvalds .error_report = clip_neigh_error, 2731da177e4SLinus Torvalds .output = dev_queue_xmit, 2741da177e4SLinus Torvalds .connected_output = dev_queue_xmit, 2751da177e4SLinus Torvalds .hh_output = dev_queue_xmit, 2761da177e4SLinus Torvalds .queue_xmit = dev_queue_xmit, 2771da177e4SLinus Torvalds }; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds static int clip_constructor(struct neighbour *neigh) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds struct atmarp_entry *entry = NEIGH2ENTRY(neigh); 2821da177e4SLinus Torvalds struct net_device *dev = neigh->dev; 2831da177e4SLinus Torvalds struct in_device *in_dev; 2841da177e4SLinus Torvalds struct neigh_parms *parms; 2851da177e4SLinus Torvalds 28699824461SJoe Perches pr_debug("(neigh %p, entry %p)\n", neigh, entry); 2876b175b26SEric W. Biederman neigh->type = inet_addr_type(&init_net, entry->ip); 288e49e76dbSStephen Hemminger if (neigh->type != RTN_UNICAST) 289e49e76dbSStephen Hemminger return -EINVAL; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds rcu_read_lock(); 292e5ed6399SHerbert Xu in_dev = __in_dev_get_rcu(dev); 2931da177e4SLinus Torvalds if (!in_dev) { 2941da177e4SLinus Torvalds rcu_read_unlock(); 2951da177e4SLinus Torvalds return -EINVAL; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds parms = in_dev->arp_parms; 2991da177e4SLinus Torvalds __neigh_parms_put(neigh->parms); 3001da177e4SLinus Torvalds neigh->parms = neigh_parms_clone(parms); 3011da177e4SLinus Torvalds rcu_read_unlock(); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds neigh->ops = &clip_neigh_ops; 3041da177e4SLinus Torvalds neigh->output = neigh->nud_state & NUD_VALID ? 3051da177e4SLinus Torvalds neigh->ops->connected_output : neigh->ops->output; 3061da177e4SLinus Torvalds entry->neigh = neigh; 3071da177e4SLinus Torvalds entry->vccs = NULL; 3081da177e4SLinus Torvalds entry->expires = jiffies - 1; 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds static u32 clip_hash(const void *pkey, const struct net_device *dev) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds return jhash_2words(*(u32 *) pkey, dev->ifindex, clip_tbl.hash_rnd); 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds static struct neigh_table clip_tbl = { 3181da177e4SLinus Torvalds .family = AF_INET, 3191da177e4SLinus Torvalds .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry), 3201da177e4SLinus Torvalds .key_len = 4, 3211da177e4SLinus Torvalds .hash = clip_hash, 3221da177e4SLinus Torvalds .constructor = clip_constructor, 3231da177e4SLinus Torvalds .id = "clip_arp_cache", 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* parameters are copied from ARP ... */ 3261da177e4SLinus Torvalds .parms = { 3271da177e4SLinus Torvalds .tbl = &clip_tbl, 3281da177e4SLinus Torvalds .base_reachable_time = 30 * HZ, 3291da177e4SLinus Torvalds .retrans_time = 1 * HZ, 3301da177e4SLinus Torvalds .gc_staletime = 60 * HZ, 3311da177e4SLinus Torvalds .reachable_time = 30 * HZ, 3321da177e4SLinus Torvalds .delay_probe_time = 5 * HZ, 3331da177e4SLinus Torvalds .queue_len = 3, 3341da177e4SLinus Torvalds .ucast_probes = 3, 3351da177e4SLinus Torvalds .mcast_probes = 3, 3361da177e4SLinus Torvalds .anycast_delay = 1 * HZ, 3371da177e4SLinus Torvalds .proxy_delay = (8 * HZ) / 10, 3381da177e4SLinus Torvalds .proxy_qlen = 64, 3391da177e4SLinus Torvalds .locktime = 1 * HZ, 3401da177e4SLinus Torvalds }, 3411da177e4SLinus Torvalds .gc_interval = 30 * HZ, 3421da177e4SLinus Torvalds .gc_thresh1 = 128, 3431da177e4SLinus Torvalds .gc_thresh2 = 512, 3441da177e4SLinus Torvalds .gc_thresh3 = 1024, 3451da177e4SLinus Torvalds }; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */ 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds /* 3501da177e4SLinus Torvalds * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means 3511da177e4SLinus Torvalds * to allocate the neighbour entry but not to ask atmarpd for resolution. Also, 3521da177e4SLinus Torvalds * don't increment the usage count. This is used to create entries in 3531da177e4SLinus Torvalds * clip_setentry. 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds static int clip_encap(struct atm_vcc *vcc, int mode) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds CLIP_VCC(vcc)->encap = mode; 3591da177e4SLinus Torvalds return 0; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3623c805a22SStephen Hemminger static netdev_tx_t clip_start_xmit(struct sk_buff *skb, 3633c805a22SStephen Hemminger struct net_device *dev) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds struct clip_priv *clip_priv = PRIV(dev); 3661da177e4SLinus Torvalds struct atmarp_entry *entry; 3671da177e4SLinus Torvalds struct atm_vcc *vcc; 3681da177e4SLinus Torvalds int old; 3691da177e4SLinus Torvalds unsigned long flags; 3701da177e4SLinus Torvalds 37199824461SJoe Perches pr_debug("(skb %p)\n", skb); 372adf30907SEric Dumazet if (!skb_dst(skb)) { 37399824461SJoe Perches pr_err("skb_dst(skb) == NULL\n"); 3741da177e4SLinus Torvalds dev_kfree_skb(skb); 3751a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 3766ed10654SPatrick McHardy return NETDEV_TX_OK; 3771da177e4SLinus Torvalds } 378adf30907SEric Dumazet if (!skb_dst(skb)->neighbour) { 3791da177e4SLinus Torvalds #if 0 380adf30907SEric Dumazet skb_dst(skb)->neighbour = clip_find_neighbour(skb_dst(skb), 1); 381adf30907SEric Dumazet if (!skb_dst(skb)->neighbour) { 3821da177e4SLinus Torvalds dev_kfree_skb(skb); /* lost that one */ 3831a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 3841da177e4SLinus Torvalds return 0; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds #endif 38799824461SJoe Perches pr_err("NO NEIGHBOUR !\n"); 3881da177e4SLinus Torvalds dev_kfree_skb(skb); 3891a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 3906ed10654SPatrick McHardy return NETDEV_TX_OK; 3911da177e4SLinus Torvalds } 392adf30907SEric Dumazet entry = NEIGH2ENTRY(skb_dst(skb)->neighbour); 3931da177e4SLinus Torvalds if (!entry->vccs) { 3941da177e4SLinus Torvalds if (time_after(jiffies, entry->expires)) { 3951da177e4SLinus Torvalds /* should be resolved */ 3961da177e4SLinus Torvalds entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ; 3971da177e4SLinus Torvalds to_atmarpd(act_need, PRIV(dev)->number, entry->ip); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS) 4001da177e4SLinus Torvalds skb_queue_tail(&entry->neigh->arp_queue, skb); 4011da177e4SLinus Torvalds else { 4021da177e4SLinus Torvalds dev_kfree_skb(skb); 4031a6afe8aSStephen Hemminger dev->stats.tx_dropped++; 4041da177e4SLinus Torvalds } 4056ed10654SPatrick McHardy return NETDEV_TX_OK; 4061da177e4SLinus Torvalds } 40752240062SStephen Hemminger pr_debug("neigh %p, vccs %p\n", entry, entry->vccs); 4081da177e4SLinus Torvalds ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc; 409adf30907SEric Dumazet pr_debug("using neighbour %p, vcc %p\n", skb_dst(skb)->neighbour, vcc); 4101da177e4SLinus Torvalds if (entry->vccs->encap) { 4111da177e4SLinus Torvalds void *here; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds here = skb_push(skb, RFC1483LLC_LEN); 4141da177e4SLinus Torvalds memcpy(here, llc_oui, sizeof(llc_oui)); 41530d492daSAl Viro ((__be16 *) here)[3] = skb->protocol; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc); 4181da177e4SLinus Torvalds ATM_SKB(skb)->atm_options = vcc->atm_options; 4191da177e4SLinus Torvalds entry->vccs->last_use = jiffies; 42052240062SStephen Hemminger pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev); 4211da177e4SLinus Torvalds old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */ 4221da177e4SLinus Torvalds if (old) { 42399824461SJoe Perches pr_warning("XOFF->XOFF transition\n"); 4246ed10654SPatrick McHardy return NETDEV_TX_OK; 4251da177e4SLinus Torvalds } 4261a6afe8aSStephen Hemminger dev->stats.tx_packets++; 4271a6afe8aSStephen Hemminger dev->stats.tx_bytes += skb->len; 4285ff765f3SStephen Hemminger vcc->send(vcc, skb); 4291da177e4SLinus Torvalds if (atm_may_send(vcc, 0)) { 4301da177e4SLinus Torvalds entry->vccs->xoff = 0; 4316ed10654SPatrick McHardy return NETDEV_TX_OK; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds spin_lock_irqsave(&clip_priv->xoff_lock, flags); 4341da177e4SLinus Torvalds netif_stop_queue(dev); /* XOFF -> throttle immediately */ 4351da177e4SLinus Torvalds barrier(); 4361da177e4SLinus Torvalds if (!entry->vccs->xoff) 4371da177e4SLinus Torvalds netif_start_queue(dev); 4381da177e4SLinus Torvalds /* Oh, we just raced with clip_pop. netif_start_queue should be 4391da177e4SLinus Torvalds good enough, because nothing should really be asleep because 4401da177e4SLinus Torvalds of the brief netif_stop_queue. If this isn't true or if it 4411da177e4SLinus Torvalds changes, use netif_wake_queue instead. */ 4421da177e4SLinus Torvalds spin_unlock_irqrestore(&clip_priv->xoff_lock, flags); 4436ed10654SPatrick McHardy return NETDEV_TX_OK; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds static int clip_mkip(struct atm_vcc *vcc, int timeout) 4471da177e4SLinus Torvalds { 448b6211ae7SDavid S. Miller struct sk_buff_head *rq, queue; 4491da177e4SLinus Torvalds struct clip_vcc *clip_vcc; 450b6211ae7SDavid S. Miller struct sk_buff *skb, *tmp; 451c40a27f4SDavid S. Miller unsigned long flags; 4521da177e4SLinus Torvalds 453e49e76dbSStephen Hemminger if (!vcc->push) 454e49e76dbSStephen Hemminger return -EBADFD; 4551da177e4SLinus Torvalds clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL); 456e49e76dbSStephen Hemminger if (!clip_vcc) 457e49e76dbSStephen Hemminger return -ENOMEM; 45899824461SJoe Perches pr_debug("%p vcc %p\n", clip_vcc, vcc); 4591da177e4SLinus Torvalds clip_vcc->vcc = vcc; 4601da177e4SLinus Torvalds vcc->user_back = clip_vcc; 4611da177e4SLinus Torvalds set_bit(ATM_VF_IS_CLIP, &vcc->flags); 4621da177e4SLinus Torvalds clip_vcc->entry = NULL; 4631da177e4SLinus Torvalds clip_vcc->xoff = 0; 4641da177e4SLinus Torvalds clip_vcc->encap = 1; 4651da177e4SLinus Torvalds clip_vcc->last_use = jiffies; 4661da177e4SLinus Torvalds clip_vcc->idle_timeout = timeout * HZ; 4671da177e4SLinus Torvalds clip_vcc->old_push = vcc->push; 4681da177e4SLinus Torvalds clip_vcc->old_pop = vcc->pop; 4691da177e4SLinus Torvalds vcc->push = clip_push; 4701da177e4SLinus Torvalds vcc->pop = clip_pop; 471c40a27f4SDavid S. Miller 472b6211ae7SDavid S. Miller __skb_queue_head_init(&queue); 473c40a27f4SDavid S. Miller rq = &sk_atm(vcc)->sk_receive_queue; 474c40a27f4SDavid S. Miller 475c40a27f4SDavid S. Miller spin_lock_irqsave(&rq->lock, flags); 476b6211ae7SDavid S. Miller skb_queue_splice_init(rq, &queue); 477c40a27f4SDavid S. Miller spin_unlock_irqrestore(&rq->lock, flags); 478c40a27f4SDavid S. Miller 4791da177e4SLinus Torvalds /* re-process everything received between connection setup and MKIP */ 480b6211ae7SDavid S. Miller skb_queue_walk_safe(&queue, skb, tmp) { 4811da177e4SLinus Torvalds if (!clip_devs) { 4821da177e4SLinus Torvalds atm_return(vcc, skb->truesize); 4831da177e4SLinus Torvalds kfree_skb(skb); 484e49e76dbSStephen Hemminger } else { 485b6211ae7SDavid S. Miller struct net_device *dev = skb->dev; 4861da177e4SLinus Torvalds unsigned int len = skb->len; 4871da177e4SLinus Torvalds 488fe26109aSYOSHIFUJI Hideaki skb_get(skb); 4891da177e4SLinus Torvalds clip_push(vcc, skb); 490b6211ae7SDavid S. Miller dev->stats.rx_packets--; 491b6211ae7SDavid S. Miller dev->stats.rx_bytes -= len; 492fe26109aSYOSHIFUJI Hideaki kfree_skb(skb); 4931da177e4SLinus Torvalds } 494c40a27f4SDavid S. Miller } 4951da177e4SLinus Torvalds return 0; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 49830d492daSAl Viro static int clip_setentry(struct atm_vcc *vcc, __be32 ip) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct neighbour *neigh; 5011da177e4SLinus Torvalds struct atmarp_entry *entry; 5021da177e4SLinus Torvalds int error; 5031da177e4SLinus Torvalds struct clip_vcc *clip_vcc; 5041da177e4SLinus Torvalds struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1}} }; 5051da177e4SLinus Torvalds struct rtable *rt; 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds if (vcc->push != clip_push) { 50899824461SJoe Perches pr_warning("non-CLIP VCC\n"); 5091da177e4SLinus Torvalds return -EBADF; 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds clip_vcc = CLIP_VCC(vcc); 5121da177e4SLinus Torvalds if (!ip) { 5131da177e4SLinus Torvalds if (!clip_vcc->entry) { 51499824461SJoe Perches pr_err("hiding hidden ATMARP entry\n"); 5151da177e4SLinus Torvalds return 0; 5161da177e4SLinus Torvalds } 51799824461SJoe Perches pr_debug("remove\n"); 5181da177e4SLinus Torvalds unlink_clip_vcc(clip_vcc); 5191da177e4SLinus Torvalds return 0; 5201da177e4SLinus Torvalds } 521f206351aSDenis V. Lunev error = ip_route_output_key(&init_net, &rt, &fl); 522e49e76dbSStephen Hemminger if (error) 523e49e76dbSStephen Hemminger return error; 5241da177e4SLinus Torvalds neigh = __neigh_lookup(&clip_tbl, &ip, rt->u.dst.dev, 1); 5251da177e4SLinus Torvalds ip_rt_put(rt); 5261da177e4SLinus Torvalds if (!neigh) 5271da177e4SLinus Torvalds return -ENOMEM; 5281da177e4SLinus Torvalds entry = NEIGH2ENTRY(neigh); 5291da177e4SLinus Torvalds if (entry != clip_vcc->entry) { 530e49e76dbSStephen Hemminger if (!clip_vcc->entry) 53199824461SJoe Perches pr_debug("add\n"); 5321da177e4SLinus Torvalds else { 53399824461SJoe Perches pr_debug("update\n"); 5341da177e4SLinus Torvalds unlink_clip_vcc(clip_vcc); 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds link_vcc(clip_vcc, entry); 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds error = neigh_update(neigh, llc_oui, NUD_PERMANENT, 5391da177e4SLinus Torvalds NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN); 5401da177e4SLinus Torvalds neigh_release(neigh); 5411da177e4SLinus Torvalds return error; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 544dde09758SStephen Hemminger static const struct net_device_ops clip_netdev_ops = { 545dde09758SStephen Hemminger .ndo_start_xmit = clip_start_xmit, 546dde09758SStephen Hemminger }; 547dde09758SStephen Hemminger 5481da177e4SLinus Torvalds static void clip_setup(struct net_device *dev) 5491da177e4SLinus Torvalds { 550dde09758SStephen Hemminger dev->netdev_ops = &clip_netdev_ops; 5511da177e4SLinus Torvalds dev->type = ARPHRD_ATM; 5521da177e4SLinus Torvalds dev->hard_header_len = RFC1483LLC_LEN; 5531da177e4SLinus Torvalds dev->mtu = RFC1626_MTU; 5541da177e4SLinus Torvalds dev->tx_queue_len = 100; /* "normal" queue (packets) */ 5551da177e4SLinus Torvalds /* When using a "real" qdisc, the qdisc determines the queue */ 5561da177e4SLinus Torvalds /* length. tx_queue_len is only used for the default case, */ 5571da177e4SLinus Torvalds /* without any more elaborate queuing. 100 is a reasonable */ 5581da177e4SLinus Torvalds /* compromise between decent burst-tolerance and protection */ 5591da177e4SLinus Torvalds /* against memory hogs. */ 560a489e51cSEric Dumazet dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds static int clip_create(int number) 5641da177e4SLinus Torvalds { 5651da177e4SLinus Torvalds struct net_device *dev; 5661da177e4SLinus Torvalds struct clip_priv *clip_priv; 5671da177e4SLinus Torvalds int error; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (number != -1) { 5701da177e4SLinus Torvalds for (dev = clip_devs; dev; dev = PRIV(dev)->next) 571e49e76dbSStephen Hemminger if (PRIV(dev)->number == number) 572e49e76dbSStephen Hemminger return -EEXIST; 573e49e76dbSStephen Hemminger } else { 5741da177e4SLinus Torvalds number = 0; 5751da177e4SLinus Torvalds for (dev = clip_devs; dev; dev = PRIV(dev)->next) 5761da177e4SLinus Torvalds if (PRIV(dev)->number >= number) 5771da177e4SLinus Torvalds number = PRIV(dev)->number + 1; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup); 5801da177e4SLinus Torvalds if (!dev) 5811da177e4SLinus Torvalds return -ENOMEM; 5821da177e4SLinus Torvalds clip_priv = PRIV(dev); 5831da177e4SLinus Torvalds sprintf(dev->name, "atm%d", number); 5841da177e4SLinus Torvalds spin_lock_init(&clip_priv->xoff_lock); 5851da177e4SLinus Torvalds clip_priv->number = number; 5861da177e4SLinus Torvalds error = register_netdev(dev); 5871da177e4SLinus Torvalds if (error) { 5881da177e4SLinus Torvalds free_netdev(dev); 5891da177e4SLinus Torvalds return error; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds clip_priv->next = clip_devs; 5921da177e4SLinus Torvalds clip_devs = dev; 59352240062SStephen Hemminger pr_debug("registered (net:%s)\n", dev->name); 5941da177e4SLinus Torvalds return number; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds static int clip_device_event(struct notifier_block *this, unsigned long event, 598f3a0592bSStephen Hemminger void *arg) 5991da177e4SLinus Torvalds { 600f3a0592bSStephen Hemminger struct net_device *dev = arg; 601f3a0592bSStephen Hemminger 602721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 603e9dc8653SEric W. Biederman return NOTIFY_DONE; 604e9dc8653SEric W. Biederman 605f3a0592bSStephen Hemminger if (event == NETDEV_UNREGISTER) { 606f3a0592bSStephen Hemminger neigh_ifdown(&clip_tbl, dev); 6071da177e4SLinus Torvalds return NOTIFY_DONE; 608f3a0592bSStephen Hemminger } 609f3a0592bSStephen Hemminger 610f3a0592bSStephen Hemminger /* ignore non-CLIP devices */ 611dde09758SStephen Hemminger if (dev->type != ARPHRD_ATM || dev->netdev_ops != &clip_netdev_ops) 612f3a0592bSStephen Hemminger return NOTIFY_DONE; 613f3a0592bSStephen Hemminger 6141da177e4SLinus Torvalds switch (event) { 6151da177e4SLinus Torvalds case NETDEV_UP: 61699824461SJoe Perches pr_debug("NETDEV_UP\n"); 6175ff765f3SStephen Hemminger to_atmarpd(act_up, PRIV(dev)->number, 0); 6181da177e4SLinus Torvalds break; 6191da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 62099824461SJoe Perches pr_debug("NETDEV_DOWN\n"); 6215ff765f3SStephen Hemminger to_atmarpd(act_down, PRIV(dev)->number, 0); 6221da177e4SLinus Torvalds break; 6231da177e4SLinus Torvalds case NETDEV_CHANGE: 6241da177e4SLinus Torvalds case NETDEV_CHANGEMTU: 62599824461SJoe Perches pr_debug("NETDEV_CHANGE*\n"); 6265ff765f3SStephen Hemminger to_atmarpd(act_change, PRIV(dev)->number, 0); 6271da177e4SLinus Torvalds break; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds return NOTIFY_DONE; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds static int clip_inet_event(struct notifier_block *this, unsigned long event, 6331da177e4SLinus Torvalds void *ifa) 6341da177e4SLinus Torvalds { 6351da177e4SLinus Torvalds struct in_device *in_dev; 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds in_dev = ((struct in_ifaddr *)ifa)->ifa_dev; 6381da177e4SLinus Torvalds /* 6391da177e4SLinus Torvalds * Transitions are of the down-change-up type, so it's sufficient to 6401da177e4SLinus Torvalds * handle the change on up. 6411da177e4SLinus Torvalds */ 642e49e76dbSStephen Hemminger if (event != NETDEV_UP) 643e49e76dbSStephen Hemminger return NOTIFY_DONE; 6441da177e4SLinus Torvalds return clip_device_event(this, NETDEV_CHANGE, in_dev->dev); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds static struct notifier_block clip_dev_notifier = { 6485ff765f3SStephen Hemminger .notifier_call = clip_device_event, 6491da177e4SLinus Torvalds }; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds static struct notifier_block clip_inet_notifier = { 6545ff765f3SStephen Hemminger .notifier_call = clip_inet_event, 6551da177e4SLinus Torvalds }; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds static void atmarpd_close(struct atm_vcc *vcc) 6601da177e4SLinus Torvalds { 66199824461SJoe Perches pr_debug("\n"); 662f3a0592bSStephen Hemminger 663f3a0592bSStephen Hemminger rtnl_lock(); 664f3a0592bSStephen Hemminger atmarpd = NULL; 6651da177e4SLinus Torvalds skb_queue_purge(&sk_atm(vcc)->sk_receive_queue); 666f3a0592bSStephen Hemminger rtnl_unlock(); 667f3a0592bSStephen Hemminger 66852240062SStephen Hemminger pr_debug("(done)\n"); 6691da177e4SLinus Torvalds module_put(THIS_MODULE); 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds static struct atmdev_ops atmarpd_dev_ops = { 6731da177e4SLinus Torvalds .close = atmarpd_close 6741da177e4SLinus Torvalds }; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds static struct atm_dev atmarpd_dev = { 6781da177e4SLinus Torvalds .ops = &atmarpd_dev_ops, 6791da177e4SLinus Torvalds .type = "arpd", 6801da177e4SLinus Torvalds .number = 999, 6814ef8d0aeSMilind Arun Choudhary .lock = __SPIN_LOCK_UNLOCKED(atmarpd_dev.lock) 6821da177e4SLinus Torvalds }; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds static int atm_init_atmarp(struct atm_vcc *vcc) 6861da177e4SLinus Torvalds { 687f3a0592bSStephen Hemminger rtnl_lock(); 688f3a0592bSStephen Hemminger if (atmarpd) { 689f3a0592bSStephen Hemminger rtnl_unlock(); 690f3a0592bSStephen Hemminger return -EADDRINUSE; 691f3a0592bSStephen Hemminger } 692f3a0592bSStephen Hemminger 6932d907392SStephen Hemminger mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ); 6942d907392SStephen Hemminger 6951da177e4SLinus Torvalds atmarpd = vcc; 6961da177e4SLinus Torvalds set_bit(ATM_VF_META, &vcc->flags); 6971da177e4SLinus Torvalds set_bit(ATM_VF_READY, &vcc->flags); 6981da177e4SLinus Torvalds /* allow replies and avoid getting closed if signaling dies */ 6991da177e4SLinus Torvalds vcc->dev = &atmarpd_dev; 7001da177e4SLinus Torvalds vcc_insert_socket(sk_atm(vcc)); 7011da177e4SLinus Torvalds vcc->push = NULL; 7021da177e4SLinus Torvalds vcc->pop = NULL; /* crash */ 7031da177e4SLinus Torvalds vcc->push_oam = NULL; /* crash */ 704f3a0592bSStephen Hemminger rtnl_unlock(); 7051da177e4SLinus Torvalds return 0; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 7091da177e4SLinus Torvalds { 7101da177e4SLinus Torvalds struct atm_vcc *vcc = ATM_SD(sock); 7111da177e4SLinus Torvalds int err = 0; 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds switch (cmd) { 7141da177e4SLinus Torvalds case SIOCMKCLIP: 7151da177e4SLinus Torvalds case ATMARPD_CTRL: 7161da177e4SLinus Torvalds case ATMARP_MKIP: 7171da177e4SLinus Torvalds case ATMARP_SETENTRY: 7181da177e4SLinus Torvalds case ATMARP_ENCAP: 7191da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 7201da177e4SLinus Torvalds return -EPERM; 7211da177e4SLinus Torvalds break; 7221da177e4SLinus Torvalds default: 7231da177e4SLinus Torvalds return -ENOIOCTLCMD; 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds switch (cmd) { 7271da177e4SLinus Torvalds case SIOCMKCLIP: 7281da177e4SLinus Torvalds err = clip_create(arg); 7291da177e4SLinus Torvalds break; 7301da177e4SLinus Torvalds case ATMARPD_CTRL: 7311da177e4SLinus Torvalds err = atm_init_atmarp(vcc); 7321da177e4SLinus Torvalds if (!err) { 7331da177e4SLinus Torvalds sock->state = SS_CONNECTED; 7341da177e4SLinus Torvalds __module_get(THIS_MODULE); 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds break; 7371da177e4SLinus Torvalds case ATMARP_MKIP: 7381da177e4SLinus Torvalds err = clip_mkip(vcc, arg); 7391da177e4SLinus Torvalds break; 7401da177e4SLinus Torvalds case ATMARP_SETENTRY: 74130d492daSAl Viro err = clip_setentry(vcc, (__force __be32)arg); 7421da177e4SLinus Torvalds break; 7431da177e4SLinus Torvalds case ATMARP_ENCAP: 7441da177e4SLinus Torvalds err = clip_encap(vcc, arg); 7451da177e4SLinus Torvalds break; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds return err; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds static struct atm_ioctl clip_ioctl_ops = { 7511da177e4SLinus Torvalds .owner = THIS_MODULE, 7521da177e4SLinus Torvalds .ioctl = clip_ioctl, 7531da177e4SLinus Torvalds }; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr) 7581da177e4SLinus Torvalds { 7591da177e4SLinus Torvalds static int code[] = { 1, 2, 10, 6, 1, 0 }; 7601da177e4SLinus Torvalds static int e164[] = { 1, 8, 4, 6, 1, 0 }; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds if (*addr->sas_addr.pub) { 7631da177e4SLinus Torvalds seq_printf(seq, "%s", addr->sas_addr.pub); 7641da177e4SLinus Torvalds if (*addr->sas_addr.prv) 7651da177e4SLinus Torvalds seq_putc(seq, '+'); 7661da177e4SLinus Torvalds } else if (!*addr->sas_addr.prv) { 7671da177e4SLinus Torvalds seq_printf(seq, "%s", "(none)"); 7681da177e4SLinus Torvalds return; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds if (*addr->sas_addr.prv) { 7711da177e4SLinus Torvalds unsigned char *prv = addr->sas_addr.prv; 7721da177e4SLinus Torvalds int *fields; 7731da177e4SLinus Torvalds int i, j; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds fields = *prv == ATM_AFI_E164 ? e164 : code; 7761da177e4SLinus Torvalds for (i = 0; fields[i]; i++) { 7771da177e4SLinus Torvalds for (j = fields[i]; j; j--) 7781da177e4SLinus Torvalds seq_printf(seq, "%02X", *prv++); 7791da177e4SLinus Torvalds if (fields[i + 1]) 7801da177e4SLinus Torvalds seq_putc(seq, '.'); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* This means the neighbour entry has no attached VCC objects. */ 7861da177e4SLinus Torvalds #define SEQ_NO_VCC_TOKEN ((void *) 2) 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds static void atmarp_info(struct seq_file *seq, struct net_device *dev, 7891da177e4SLinus Torvalds struct atmarp_entry *entry, struct clip_vcc *clip_vcc) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds unsigned long exp; 7921da177e4SLinus Torvalds char buf[17]; 7931da177e4SLinus Torvalds int svc, llc, off; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || 7961da177e4SLinus Torvalds (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC)); 7971da177e4SLinus Torvalds 798e49e76dbSStephen Hemminger llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap); 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (clip_vcc == SEQ_NO_VCC_TOKEN) 8011da177e4SLinus Torvalds exp = entry->neigh->used; 8021da177e4SLinus Torvalds else 8031da177e4SLinus Torvalds exp = clip_vcc->last_use; 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds exp = (jiffies - exp) / HZ; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds seq_printf(seq, "%-6s%-4s%-4s%5ld ", 808e49e76dbSStephen Hemminger dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp); 8091da177e4SLinus Torvalds 81021454aaaSHarvey Harrison off = scnprintf(buf, sizeof(buf) - 1, "%pI4", 81121454aaaSHarvey Harrison &entry->ip); 8121da177e4SLinus Torvalds while (off < 16) 8131da177e4SLinus Torvalds buf[off++] = ' '; 8141da177e4SLinus Torvalds buf[off] = '\0'; 8151da177e4SLinus Torvalds seq_printf(seq, "%s", buf); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds if (clip_vcc == SEQ_NO_VCC_TOKEN) { 8181da177e4SLinus Torvalds if (time_before(jiffies, entry->expires)) 8191da177e4SLinus Torvalds seq_printf(seq, "(resolving)\n"); 8201da177e4SLinus Torvalds else 8211da177e4SLinus Torvalds seq_printf(seq, "(expired, ref %d)\n", 8221da177e4SLinus Torvalds atomic_read(&entry->neigh->refcnt)); 8231da177e4SLinus Torvalds } else if (!svc) { 8241da177e4SLinus Torvalds seq_printf(seq, "%d.%d.%d\n", 8251da177e4SLinus Torvalds clip_vcc->vcc->dev->number, 826e49e76dbSStephen Hemminger clip_vcc->vcc->vpi, clip_vcc->vcc->vci); 8271da177e4SLinus Torvalds } else { 8281da177e4SLinus Torvalds svc_addr(seq, &clip_vcc->vcc->remote); 8291da177e4SLinus Torvalds seq_putc(seq, '\n'); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds struct clip_seq_state { 8341da177e4SLinus Torvalds /* This member must be first. */ 8351da177e4SLinus Torvalds struct neigh_seq_state ns; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* Local to clip specific iteration. */ 8381da177e4SLinus Torvalds struct clip_vcc *vcc; 8391da177e4SLinus Torvalds }; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e, 8421da177e4SLinus Torvalds struct clip_vcc *curr) 8431da177e4SLinus Torvalds { 8441da177e4SLinus Torvalds if (!curr) { 8451da177e4SLinus Torvalds curr = e->vccs; 8461da177e4SLinus Torvalds if (!curr) 8471da177e4SLinus Torvalds return SEQ_NO_VCC_TOKEN; 8481da177e4SLinus Torvalds return curr; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds if (curr == SEQ_NO_VCC_TOKEN) 8511da177e4SLinus Torvalds return NULL; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds curr = curr->next; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds return curr; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds static void *clip_seq_vcc_walk(struct clip_seq_state *state, 8591da177e4SLinus Torvalds struct atmarp_entry *e, loff_t * pos) 8601da177e4SLinus Torvalds { 8611da177e4SLinus Torvalds struct clip_vcc *vcc = state->vcc; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds vcc = clip_seq_next_vcc(e, vcc); 8641da177e4SLinus Torvalds if (vcc && pos != NULL) { 8651da177e4SLinus Torvalds while (*pos) { 8661da177e4SLinus Torvalds vcc = clip_seq_next_vcc(e, vcc); 8671da177e4SLinus Torvalds if (!vcc) 8681da177e4SLinus Torvalds break; 8691da177e4SLinus Torvalds --(*pos); 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds state->vcc = vcc; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds return vcc; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds static void *clip_seq_sub_iter(struct neigh_seq_state *_state, 8781da177e4SLinus Torvalds struct neighbour *n, loff_t * pos) 8791da177e4SLinus Torvalds { 8801da177e4SLinus Torvalds struct clip_seq_state *state = (struct clip_seq_state *)_state; 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos); 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds static void *clip_seq_start(struct seq_file *seq, loff_t * pos) 8861da177e4SLinus Torvalds { 887ae22120aSDenis V. Lunev struct clip_seq_state *state = seq->private; 888ae22120aSDenis V. Lunev state->ns.neigh_sub_iter = clip_seq_sub_iter; 8891da177e4SLinus Torvalds return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY); 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds static int clip_seq_show(struct seq_file *seq, void *v) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds static char atm_arp_banner[] = 8951da177e4SLinus Torvalds "IPitf TypeEncp Idle IP address ATM address\n"; 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds if (v == SEQ_START_TOKEN) { 8981da177e4SLinus Torvalds seq_puts(seq, atm_arp_banner); 8991da177e4SLinus Torvalds } else { 9001da177e4SLinus Torvalds struct clip_seq_state *state = seq->private; 9011da177e4SLinus Torvalds struct neighbour *n = v; 9021da177e4SLinus Torvalds struct clip_vcc *vcc = state->vcc; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc); 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds return 0; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 90956b3d975SPhilippe De Muyter static const struct seq_operations arp_seq_ops = { 9101da177e4SLinus Torvalds .start = clip_seq_start, 9111da177e4SLinus Torvalds .next = neigh_seq_next, 9121da177e4SLinus Torvalds .stop = neigh_seq_stop, 9131da177e4SLinus Torvalds .show = clip_seq_show, 9141da177e4SLinus Torvalds }; 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds static int arp_seq_open(struct inode *inode, struct file *file) 9171da177e4SLinus Torvalds { 918ae22120aSDenis V. Lunev return seq_open_net(inode, file, &arp_seq_ops, 919ae22120aSDenis V. Lunev sizeof(struct clip_seq_state)); 920426b5303SEric W. Biederman } 921426b5303SEric W. Biederman 9229a32144eSArjan van de Ven static const struct file_operations arp_seq_fops = { 9231da177e4SLinus Torvalds .open = arp_seq_open, 9241da177e4SLinus Torvalds .read = seq_read, 9251da177e4SLinus Torvalds .llseek = seq_lseek, 926ae22120aSDenis V. Lunev .release = seq_release_net, 9271da177e4SLinus Torvalds .owner = THIS_MODULE 9281da177e4SLinus Torvalds }; 9291da177e4SLinus Torvalds #endif 9301da177e4SLinus Torvalds 931dbee0d3fSWang Chen static void atm_clip_exit_noproc(void); 932dbee0d3fSWang Chen 9331da177e4SLinus Torvalds static int __init atm_clip_init(void) 9341da177e4SLinus Torvalds { 935bd89efc5SSimon Kelley neigh_table_init_no_netlink(&clip_tbl); 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds clip_tbl_hook = &clip_tbl; 9381da177e4SLinus Torvalds register_atm_ioctl(&clip_ioctl_ops); 939f3a0592bSStephen Hemminger register_netdevice_notifier(&clip_dev_notifier); 940f3a0592bSStephen Hemminger register_inetaddr_notifier(&clip_inet_notifier); 9411da177e4SLinus Torvalds 9422d907392SStephen Hemminger setup_timer(&idle_timer, idle_timer_check, 0); 9432d907392SStephen Hemminger 94424781734SAdrian Bunk #ifdef CONFIG_PROC_FS 94524781734SAdrian Bunk { 94624781734SAdrian Bunk struct proc_dir_entry *p; 94724781734SAdrian Bunk 94816e297b3SWang Chen p = proc_create("arp", S_IRUGO, atm_proc_root, &arp_seq_fops); 949dbee0d3fSWang Chen if (!p) { 95099824461SJoe Perches pr_err("Unable to initialize /proc/net/atm/arp\n"); 951dbee0d3fSWang Chen atm_clip_exit_noproc(); 952dbee0d3fSWang Chen return -ENOMEM; 953dbee0d3fSWang Chen } 95424781734SAdrian Bunk } 95524781734SAdrian Bunk #endif 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds return 0; 9581da177e4SLinus Torvalds } 9591da177e4SLinus Torvalds 960dbee0d3fSWang Chen static void atm_clip_exit_noproc(void) 9611da177e4SLinus Torvalds { 9621da177e4SLinus Torvalds struct net_device *dev, *next; 9631da177e4SLinus Torvalds 964f3a0592bSStephen Hemminger unregister_inetaddr_notifier(&clip_inet_notifier); 965f3a0592bSStephen Hemminger unregister_netdevice_notifier(&clip_dev_notifier); 966f3a0592bSStephen Hemminger 9671da177e4SLinus Torvalds deregister_atm_ioctl(&clip_ioctl_ops); 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* First, stop the idle timer, so it stops banging 9701da177e4SLinus Torvalds * on the table. 9711da177e4SLinus Torvalds */ 9722d907392SStephen Hemminger del_timer_sync(&idle_timer); 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds /* Next, purge the table, so that the device 9751da177e4SLinus Torvalds * unregister loop below does not hang due to 9761da177e4SLinus Torvalds * device references remaining in the table. 9771da177e4SLinus Torvalds */ 9781da177e4SLinus Torvalds neigh_ifdown(&clip_tbl, NULL); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds dev = clip_devs; 9811da177e4SLinus Torvalds while (dev) { 9821da177e4SLinus Torvalds next = PRIV(dev)->next; 9831da177e4SLinus Torvalds unregister_netdev(dev); 9841da177e4SLinus Torvalds free_netdev(dev); 9851da177e4SLinus Torvalds dev = next; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds /* Now it is safe to fully shutdown whole table. */ 9891da177e4SLinus Torvalds neigh_table_clear(&clip_tbl); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds clip_tbl_hook = NULL; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 994dbee0d3fSWang Chen static void __exit atm_clip_exit(void) 995dbee0d3fSWang Chen { 996dbee0d3fSWang Chen remove_proc_entry("arp", atm_proc_root); 997dbee0d3fSWang Chen 998dbee0d3fSWang Chen atm_clip_exit_noproc(); 999dbee0d3fSWang Chen } 1000dbee0d3fSWang Chen 10011da177e4SLinus Torvalds module_init(atm_clip_init); 10021da177e4SLinus Torvalds module_exit(atm_clip_exit); 10034909e488SStephen Hemminger MODULE_AUTHOR("Werner Almesberger"); 10044909e488SStephen Hemminger MODULE_DESCRIPTION("Classical/IP over ATM interface"); 10051da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1006