clip.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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_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. .solicit = clip_neigh_solicit,
  246. .error_report = clip_neigh_error,
  247. .output = dev_queue_xmit,
  248. .connected_output = dev_queue_xmit,
  249. .hh_output = dev_queue_xmit,
  250. .queue_xmit = dev_queue_xmit,
  251. };
  252. static int clip_constructor(struct neighbour *neigh)
  253. {
  254. struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
  255. struct net_device *dev = neigh->dev;
  256. struct in_device *in_dev;
  257. struct neigh_parms *parms;
  258. DPRINTK("clip_constructor (neigh %p, entry %p)\n", neigh, entry);
  259. neigh->type = inet_addr_type(entry->ip);
  260. if (neigh->type != RTN_UNICAST)
  261. return -EINVAL;
  262. rcu_read_lock();
  263. in_dev = __in_dev_get_rcu(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. ((__be16 *) 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. 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 *skb;
  412. struct sk_buff_head *rq;
  413. unsigned long flags;
  414. if (!vcc->push)
  415. return -EBADFD;
  416. clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL);
  417. if (!clip_vcc)
  418. return -ENOMEM;
  419. DPRINTK("mkip clip_vcc %p vcc %p\n", clip_vcc, vcc);
  420. clip_vcc->vcc = vcc;
  421. vcc->user_back = clip_vcc;
  422. set_bit(ATM_VF_IS_CLIP, &vcc->flags);
  423. clip_vcc->entry = NULL;
  424. clip_vcc->xoff = 0;
  425. clip_vcc->encap = 1;
  426. clip_vcc->last_use = jiffies;
  427. clip_vcc->idle_timeout = timeout * HZ;
  428. clip_vcc->old_push = vcc->push;
  429. clip_vcc->old_pop = vcc->pop;
  430. vcc->push = clip_push;
  431. vcc->pop = clip_pop;
  432. rq = &sk_atm(vcc)->sk_receive_queue;
  433. spin_lock_irqsave(&rq->lock, flags);
  434. if (skb_queue_empty(rq)) {
  435. skb = NULL;
  436. } else {
  437. /* NULL terminate the list. */
  438. rq->prev->next = NULL;
  439. skb = rq->next;
  440. }
  441. rq->prev = rq->next = (struct sk_buff *)rq;
  442. rq->qlen = 0;
  443. spin_unlock_irqrestore(&rq->lock, flags);
  444. /* re-process everything received between connection setup and MKIP */
  445. while (skb) {
  446. struct sk_buff *next = skb->next;
  447. skb->next = skb->prev = NULL;
  448. if (!clip_devs) {
  449. atm_return(vcc, skb->truesize);
  450. kfree_skb(skb);
  451. } else {
  452. unsigned int len = skb->len;
  453. skb_get(skb);
  454. clip_push(vcc, skb);
  455. PRIV(skb->dev)->stats.rx_packets--;
  456. PRIV(skb->dev)->stats.rx_bytes -= len;
  457. kfree_skb(skb);
  458. }
  459. skb = next;
  460. }
  461. return 0;
  462. }
  463. static int clip_setentry(struct atm_vcc *vcc, __be32 ip)
  464. {
  465. struct neighbour *neigh;
  466. struct atmarp_entry *entry;
  467. int error;
  468. struct clip_vcc *clip_vcc;
  469. struct flowi fl = { .nl_u = { .ip4_u = { .daddr = ip, .tos = 1}} };
  470. struct rtable *rt;
  471. if (vcc->push != clip_push) {
  472. printk(KERN_WARNING "clip_setentry: non-CLIP VCC\n");
  473. return -EBADF;
  474. }
  475. clip_vcc = CLIP_VCC(vcc);
  476. if (!ip) {
  477. if (!clip_vcc->entry) {
  478. printk(KERN_ERR "hiding hidden ATMARP entry\n");
  479. return 0;
  480. }
  481. DPRINTK("setentry: remove\n");
  482. unlink_clip_vcc(clip_vcc);
  483. return 0;
  484. }
  485. error = ip_route_output_key(&rt, &fl);
  486. if (error)
  487. return error;
  488. neigh = __neigh_lookup(&clip_tbl, &ip, rt->u.dst.dev, 1);
  489. ip_rt_put(rt);
  490. if (!neigh)
  491. return -ENOMEM;
  492. entry = NEIGH2ENTRY(neigh);
  493. if (entry != clip_vcc->entry) {
  494. if (!clip_vcc->entry)
  495. DPRINTK("setentry: add\n");
  496. else {
  497. DPRINTK("setentry: update\n");
  498. unlink_clip_vcc(clip_vcc);
  499. }
  500. link_vcc(clip_vcc, entry);
  501. }
  502. error = neigh_update(neigh, llc_oui, NUD_PERMANENT,
  503. NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN);
  504. neigh_release(neigh);
  505. return error;
  506. }
  507. static void clip_setup(struct net_device *dev)
  508. {
  509. dev->hard_start_xmit = clip_start_xmit;
  510. /* sg_xmit ... */
  511. dev->get_stats = clip_get_stats;
  512. dev->type = ARPHRD_ATM;
  513. dev->hard_header_len = RFC1483LLC_LEN;
  514. dev->mtu = RFC1626_MTU;
  515. dev->tx_queue_len = 100; /* "normal" queue (packets) */
  516. /* When using a "real" qdisc, the qdisc determines the queue */
  517. /* length. tx_queue_len is only used for the default case, */
  518. /* without any more elaborate queuing. 100 is a reasonable */
  519. /* compromise between decent burst-tolerance and protection */
  520. /* against memory hogs. */
  521. }
  522. static int clip_create(int number)
  523. {
  524. struct net_device *dev;
  525. struct clip_priv *clip_priv;
  526. int error;
  527. if (number != -1) {
  528. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  529. if (PRIV(dev)->number == number)
  530. return -EEXIST;
  531. } else {
  532. number = 0;
  533. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  534. if (PRIV(dev)->number >= number)
  535. number = PRIV(dev)->number + 1;
  536. }
  537. dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup);
  538. if (!dev)
  539. return -ENOMEM;
  540. clip_priv = PRIV(dev);
  541. sprintf(dev->name, "atm%d", number);
  542. spin_lock_init(&clip_priv->xoff_lock);
  543. clip_priv->number = number;
  544. error = register_netdev(dev);
  545. if (error) {
  546. free_netdev(dev);
  547. return error;
  548. }
  549. clip_priv->next = clip_devs;
  550. clip_devs = dev;
  551. DPRINTK("registered (net:%s)\n", dev->name);
  552. return number;
  553. }
  554. static int clip_device_event(struct notifier_block *this, unsigned long event,
  555. void *arg)
  556. {
  557. struct net_device *dev = arg;
  558. if (event == NETDEV_UNREGISTER) {
  559. neigh_ifdown(&clip_tbl, dev);
  560. return NOTIFY_DONE;
  561. }
  562. /* ignore non-CLIP devices */
  563. if (dev->type != ARPHRD_ATM || dev->hard_start_xmit != clip_start_xmit)
  564. return NOTIFY_DONE;
  565. switch (event) {
  566. case NETDEV_UP:
  567. DPRINTK("clip_device_event NETDEV_UP\n");
  568. to_atmarpd(act_up, PRIV(dev)->number, 0);
  569. break;
  570. case NETDEV_GOING_DOWN:
  571. DPRINTK("clip_device_event NETDEV_DOWN\n");
  572. to_atmarpd(act_down, PRIV(dev)->number, 0);
  573. break;
  574. case NETDEV_CHANGE:
  575. case NETDEV_CHANGEMTU:
  576. DPRINTK("clip_device_event NETDEV_CHANGE*\n");
  577. to_atmarpd(act_change, PRIV(dev)->number, 0);
  578. break;
  579. }
  580. return NOTIFY_DONE;
  581. }
  582. static int clip_inet_event(struct notifier_block *this, unsigned long event,
  583. void *ifa)
  584. {
  585. struct in_device *in_dev;
  586. in_dev = ((struct in_ifaddr *)ifa)->ifa_dev;
  587. if (!in_dev || !in_dev->dev) {
  588. printk(KERN_WARNING "clip_inet_event: no device\n");
  589. return NOTIFY_DONE;
  590. }
  591. /*
  592. * Transitions are of the down-change-up type, so it's sufficient to
  593. * handle the change on up.
  594. */
  595. if (event != NETDEV_UP)
  596. return NOTIFY_DONE;
  597. return clip_device_event(this, NETDEV_CHANGE, in_dev->dev);
  598. }
  599. static struct notifier_block clip_dev_notifier = {
  600. .notifier_call = clip_device_event,
  601. };
  602. static struct notifier_block clip_inet_notifier = {
  603. .notifier_call = clip_inet_event,
  604. };
  605. static void atmarpd_close(struct atm_vcc *vcc)
  606. {
  607. DPRINTK("atmarpd_close\n");
  608. rtnl_lock();
  609. atmarpd = NULL;
  610. skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
  611. rtnl_unlock();
  612. DPRINTK("(done)\n");
  613. module_put(THIS_MODULE);
  614. }
  615. static struct atmdev_ops atmarpd_dev_ops = {
  616. .close = atmarpd_close
  617. };
  618. static struct atm_dev atmarpd_dev = {
  619. .ops = &atmarpd_dev_ops,
  620. .type = "arpd",
  621. .number = 999,
  622. .lock = SPIN_LOCK_UNLOCKED
  623. };
  624. static int atm_init_atmarp(struct atm_vcc *vcc)
  625. {
  626. rtnl_lock();
  627. if (atmarpd) {
  628. rtnl_unlock();
  629. return -EADDRINUSE;
  630. }
  631. mod_timer(&idle_timer, jiffies+CLIP_CHECK_INTERVAL*HZ);
  632. atmarpd = vcc;
  633. set_bit(ATM_VF_META,&vcc->flags);
  634. set_bit(ATM_VF_READY,&vcc->flags);
  635. /* allow replies and avoid getting closed if signaling dies */
  636. vcc->dev = &atmarpd_dev;
  637. vcc_insert_socket(sk_atm(vcc));
  638. vcc->push = NULL;
  639. vcc->pop = NULL; /* crash */
  640. vcc->push_oam = NULL; /* crash */
  641. rtnl_unlock();
  642. return 0;
  643. }
  644. static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  645. {
  646. struct atm_vcc *vcc = ATM_SD(sock);
  647. int err = 0;
  648. switch (cmd) {
  649. case SIOCMKCLIP:
  650. case ATMARPD_CTRL:
  651. case ATMARP_MKIP:
  652. case ATMARP_SETENTRY:
  653. case ATMARP_ENCAP:
  654. if (!capable(CAP_NET_ADMIN))
  655. return -EPERM;
  656. break;
  657. default:
  658. return -ENOIOCTLCMD;
  659. }
  660. switch (cmd) {
  661. case SIOCMKCLIP:
  662. err = clip_create(arg);
  663. break;
  664. case ATMARPD_CTRL:
  665. err = atm_init_atmarp(vcc);
  666. if (!err) {
  667. sock->state = SS_CONNECTED;
  668. __module_get(THIS_MODULE);
  669. }
  670. break;
  671. case ATMARP_MKIP:
  672. err = clip_mkip(vcc, arg);
  673. break;
  674. case ATMARP_SETENTRY:
  675. err = clip_setentry(vcc, (__force __be32)arg);
  676. break;
  677. case ATMARP_ENCAP:
  678. err = clip_encap(vcc, arg);
  679. break;
  680. }
  681. return err;
  682. }
  683. static struct atm_ioctl clip_ioctl_ops = {
  684. .owner = THIS_MODULE,
  685. .ioctl = clip_ioctl,
  686. };
  687. #ifdef CONFIG_PROC_FS
  688. static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
  689. {
  690. static int code[] = { 1, 2, 10, 6, 1, 0 };
  691. static int e164[] = { 1, 8, 4, 6, 1, 0 };
  692. if (*addr->sas_addr.pub) {
  693. seq_printf(seq, "%s", addr->sas_addr.pub);
  694. if (*addr->sas_addr.prv)
  695. seq_putc(seq, '+');
  696. } else if (!*addr->sas_addr.prv) {
  697. seq_printf(seq, "%s", "(none)");
  698. return;
  699. }
  700. if (*addr->sas_addr.prv) {
  701. unsigned char *prv = addr->sas_addr.prv;
  702. int *fields;
  703. int i, j;
  704. fields = *prv == ATM_AFI_E164 ? e164 : code;
  705. for (i = 0; fields[i]; i++) {
  706. for (j = fields[i]; j; j--)
  707. seq_printf(seq, "%02X", *prv++);
  708. if (fields[i + 1])
  709. seq_putc(seq, '.');
  710. }
  711. }
  712. }
  713. /* This means the neighbour entry has no attached VCC objects. */
  714. #define SEQ_NO_VCC_TOKEN ((void *) 2)
  715. static void atmarp_info(struct seq_file *seq, struct net_device *dev,
  716. struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
  717. {
  718. unsigned long exp;
  719. char buf[17];
  720. int svc, llc, off;
  721. svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
  722. (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC));
  723. llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap);
  724. if (clip_vcc == SEQ_NO_VCC_TOKEN)
  725. exp = entry->neigh->used;
  726. else
  727. exp = clip_vcc->last_use;
  728. exp = (jiffies - exp) / HZ;
  729. seq_printf(seq, "%-6s%-4s%-4s%5ld ",
  730. dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp);
  731. off = scnprintf(buf, sizeof(buf) - 1, "%d.%d.%d.%d",
  732. NIPQUAD(entry->ip));
  733. while (off < 16)
  734. buf[off++] = ' ';
  735. buf[off] = '\0';
  736. seq_printf(seq, "%s", buf);
  737. if (clip_vcc == SEQ_NO_VCC_TOKEN) {
  738. if (time_before(jiffies, entry->expires))
  739. seq_printf(seq, "(resolving)\n");
  740. else
  741. seq_printf(seq, "(expired, ref %d)\n",
  742. atomic_read(&entry->neigh->refcnt));
  743. } else if (!svc) {
  744. seq_printf(seq, "%d.%d.%d\n",
  745. clip_vcc->vcc->dev->number,
  746. clip_vcc->vcc->vpi, clip_vcc->vcc->vci);
  747. } else {
  748. svc_addr(seq, &clip_vcc->vcc->remote);
  749. seq_putc(seq, '\n');
  750. }
  751. }
  752. struct clip_seq_state {
  753. /* This member must be first. */
  754. struct neigh_seq_state ns;
  755. /* Local to clip specific iteration. */
  756. struct clip_vcc *vcc;
  757. };
  758. static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e,
  759. struct clip_vcc *curr)
  760. {
  761. if (!curr) {
  762. curr = e->vccs;
  763. if (!curr)
  764. return SEQ_NO_VCC_TOKEN;
  765. return curr;
  766. }
  767. if (curr == SEQ_NO_VCC_TOKEN)
  768. return NULL;
  769. curr = curr->next;
  770. return curr;
  771. }
  772. static void *clip_seq_vcc_walk(struct clip_seq_state *state,
  773. struct atmarp_entry *e, loff_t * pos)
  774. {
  775. struct clip_vcc *vcc = state->vcc;
  776. vcc = clip_seq_next_vcc(e, vcc);
  777. if (vcc && pos != NULL) {
  778. while (*pos) {
  779. vcc = clip_seq_next_vcc(e, vcc);
  780. if (!vcc)
  781. break;
  782. --(*pos);
  783. }
  784. }
  785. state->vcc = vcc;
  786. return vcc;
  787. }
  788. static void *clip_seq_sub_iter(struct neigh_seq_state *_state,
  789. struct neighbour *n, loff_t * pos)
  790. {
  791. struct clip_seq_state *state = (struct clip_seq_state *)_state;
  792. return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos);
  793. }
  794. static void *clip_seq_start(struct seq_file *seq, loff_t * pos)
  795. {
  796. return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY);
  797. }
  798. static int clip_seq_show(struct seq_file *seq, void *v)
  799. {
  800. static char atm_arp_banner[] =
  801. "IPitf TypeEncp Idle IP address ATM address\n";
  802. if (v == SEQ_START_TOKEN) {
  803. seq_puts(seq, atm_arp_banner);
  804. } else {
  805. struct clip_seq_state *state = seq->private;
  806. struct neighbour *n = v;
  807. struct clip_vcc *vcc = state->vcc;
  808. atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc);
  809. }
  810. return 0;
  811. }
  812. static struct seq_operations arp_seq_ops = {
  813. .start = clip_seq_start,
  814. .next = neigh_seq_next,
  815. .stop = neigh_seq_stop,
  816. .show = clip_seq_show,
  817. };
  818. static int arp_seq_open(struct inode *inode, struct file *file)
  819. {
  820. struct clip_seq_state *state;
  821. struct seq_file *seq;
  822. int rc = -EAGAIN;
  823. state = kzalloc(sizeof(*state), GFP_KERNEL);
  824. if (!state) {
  825. rc = -ENOMEM;
  826. goto out_kfree;
  827. }
  828. state->ns.neigh_sub_iter = clip_seq_sub_iter;
  829. rc = seq_open(file, &arp_seq_ops);
  830. if (rc)
  831. goto out_kfree;
  832. seq = file->private_data;
  833. seq->private = state;
  834. out:
  835. return rc;
  836. out_kfree:
  837. kfree(state);
  838. goto out;
  839. }
  840. static const struct file_operations arp_seq_fops = {
  841. .open = arp_seq_open,
  842. .read = seq_read,
  843. .llseek = seq_lseek,
  844. .release = seq_release_private,
  845. .owner = THIS_MODULE
  846. };
  847. #endif
  848. static int __init atm_clip_init(void)
  849. {
  850. neigh_table_init_no_netlink(&clip_tbl);
  851. clip_tbl_hook = &clip_tbl;
  852. register_atm_ioctl(&clip_ioctl_ops);
  853. register_netdevice_notifier(&clip_dev_notifier);
  854. register_inetaddr_notifier(&clip_inet_notifier);
  855. setup_timer(&idle_timer, idle_timer_check, 0);
  856. #ifdef CONFIG_PROC_FS
  857. {
  858. struct proc_dir_entry *p;
  859. p = create_proc_entry("arp", S_IRUGO, atm_proc_root);
  860. if (p)
  861. p->proc_fops = &arp_seq_fops;
  862. }
  863. #endif
  864. return 0;
  865. }
  866. static void __exit atm_clip_exit(void)
  867. {
  868. struct net_device *dev, *next;
  869. remove_proc_entry("arp", atm_proc_root);
  870. unregister_inetaddr_notifier(&clip_inet_notifier);
  871. unregister_netdevice_notifier(&clip_dev_notifier);
  872. deregister_atm_ioctl(&clip_ioctl_ops);
  873. /* First, stop the idle timer, so it stops banging
  874. * on the table.
  875. */
  876. del_timer_sync(&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_AUTHOR("Werner Almesberger");
  896. MODULE_DESCRIPTION("Classical/IP over ATM interface");
  897. MODULE_LICENSE("GPL");