clip.c 24 KB

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