clip.c 25 KB

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