clip.c 24 KB

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