clip.c 25 KB

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