clip.c 25 KB

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