clip.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. .queue_xmit = dev_queue_xmit,
  244. };
  245. static int clip_constructor(struct neighbour *neigh)
  246. {
  247. struct atmarp_entry *entry = NEIGH2ENTRY(neigh);
  248. struct net_device *dev = neigh->dev;
  249. struct in_device *in_dev;
  250. struct neigh_parms *parms;
  251. pr_debug("(neigh %p, entry %p)\n", neigh, entry);
  252. neigh->type = inet_addr_type(&init_net, entry->ip);
  253. if (neigh->type != RTN_UNICAST)
  254. return -EINVAL;
  255. rcu_read_lock();
  256. in_dev = __in_dev_get_rcu(dev);
  257. if (!in_dev) {
  258. rcu_read_unlock();
  259. return -EINVAL;
  260. }
  261. parms = in_dev->arp_parms;
  262. __neigh_parms_put(neigh->parms);
  263. neigh->parms = neigh_parms_clone(parms);
  264. rcu_read_unlock();
  265. neigh->ops = &clip_neigh_ops;
  266. neigh->output = neigh->nud_state & NUD_VALID ?
  267. neigh->ops->connected_output : neigh->ops->output;
  268. entry->neigh = neigh;
  269. entry->vccs = NULL;
  270. entry->expires = jiffies - 1;
  271. return 0;
  272. }
  273. static u32 clip_hash(const void *pkey, const struct net_device *dev, __u32 rnd)
  274. {
  275. return jhash_2words(*(u32 *) pkey, dev->ifindex, rnd);
  276. }
  277. static struct neigh_table clip_tbl = {
  278. .family = AF_INET,
  279. .entry_size = sizeof(struct neighbour)+sizeof(struct atmarp_entry),
  280. .key_len = 4,
  281. .hash = clip_hash,
  282. .constructor = clip_constructor,
  283. .id = "clip_arp_cache",
  284. /* parameters are copied from ARP ... */
  285. .parms = {
  286. .tbl = &clip_tbl,
  287. .base_reachable_time = 30 * HZ,
  288. .retrans_time = 1 * HZ,
  289. .gc_staletime = 60 * HZ,
  290. .reachable_time = 30 * HZ,
  291. .delay_probe_time = 5 * HZ,
  292. .queue_len = 3,
  293. .ucast_probes = 3,
  294. .mcast_probes = 3,
  295. .anycast_delay = 1 * HZ,
  296. .proxy_delay = (8 * HZ) / 10,
  297. .proxy_qlen = 64,
  298. .locktime = 1 * HZ,
  299. },
  300. .gc_interval = 30 * HZ,
  301. .gc_thresh1 = 128,
  302. .gc_thresh2 = 512,
  303. .gc_thresh3 = 1024,
  304. };
  305. /* @@@ copy bh locking from arp.c -- need to bh-enable atm code before */
  306. /*
  307. * We play with the resolve flag: 0 and 1 have the usual meaning, but -1 means
  308. * to allocate the neighbour entry but not to ask atmarpd for resolution. Also,
  309. * don't increment the usage count. This is used to create entries in
  310. * clip_setentry.
  311. */
  312. static int clip_encap(struct atm_vcc *vcc, int mode)
  313. {
  314. CLIP_VCC(vcc)->encap = mode;
  315. return 0;
  316. }
  317. static netdev_tx_t clip_start_xmit(struct sk_buff *skb,
  318. struct net_device *dev)
  319. {
  320. struct clip_priv *clip_priv = PRIV(dev);
  321. struct atmarp_entry *entry;
  322. struct atm_vcc *vcc;
  323. int old;
  324. unsigned long flags;
  325. pr_debug("(skb %p)\n", skb);
  326. if (!skb_dst(skb)) {
  327. pr_err("skb_dst(skb) == NULL\n");
  328. dev_kfree_skb(skb);
  329. dev->stats.tx_dropped++;
  330. return NETDEV_TX_OK;
  331. }
  332. if (!skb_dst(skb)->neighbour) {
  333. #if 0
  334. skb_dst(skb)->neighbour = clip_find_neighbour(skb_dst(skb), 1);
  335. if (!skb_dst(skb)->neighbour) {
  336. dev_kfree_skb(skb); /* lost that one */
  337. dev->stats.tx_dropped++;
  338. return 0;
  339. }
  340. #endif
  341. pr_err("NO NEIGHBOUR !\n");
  342. dev_kfree_skb(skb);
  343. dev->stats.tx_dropped++;
  344. return NETDEV_TX_OK;
  345. }
  346. entry = NEIGH2ENTRY(skb_dst(skb)->neighbour);
  347. if (!entry->vccs) {
  348. if (time_after(jiffies, entry->expires)) {
  349. /* should be resolved */
  350. entry->expires = jiffies + ATMARP_RETRY_DELAY * HZ;
  351. to_atmarpd(act_need, PRIV(dev)->number, entry->ip);
  352. }
  353. if (entry->neigh->arp_queue.qlen < ATMARP_MAX_UNRES_PACKETS)
  354. skb_queue_tail(&entry->neigh->arp_queue, skb);
  355. else {
  356. dev_kfree_skb(skb);
  357. dev->stats.tx_dropped++;
  358. }
  359. return NETDEV_TX_OK;
  360. }
  361. pr_debug("neigh %p, vccs %p\n", entry, entry->vccs);
  362. ATM_SKB(skb)->vcc = vcc = entry->vccs->vcc;
  363. pr_debug("using neighbour %p, vcc %p\n", skb_dst(skb)->neighbour, vcc);
  364. if (entry->vccs->encap) {
  365. void *here;
  366. here = skb_push(skb, RFC1483LLC_LEN);
  367. memcpy(here, llc_oui, sizeof(llc_oui));
  368. ((__be16 *) here)[3] = skb->protocol;
  369. }
  370. atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
  371. ATM_SKB(skb)->atm_options = vcc->atm_options;
  372. entry->vccs->last_use = jiffies;
  373. pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, vcc, vcc->dev);
  374. old = xchg(&entry->vccs->xoff, 1); /* assume XOFF ... */
  375. if (old) {
  376. pr_warning("XOFF->XOFF transition\n");
  377. return NETDEV_TX_OK;
  378. }
  379. dev->stats.tx_packets++;
  380. dev->stats.tx_bytes += skb->len;
  381. vcc->send(vcc, skb);
  382. if (atm_may_send(vcc, 0)) {
  383. entry->vccs->xoff = 0;
  384. return NETDEV_TX_OK;
  385. }
  386. spin_lock_irqsave(&clip_priv->xoff_lock, flags);
  387. netif_stop_queue(dev); /* XOFF -> throttle immediately */
  388. barrier();
  389. if (!entry->vccs->xoff)
  390. netif_start_queue(dev);
  391. /* Oh, we just raced with clip_pop. netif_start_queue should be
  392. good enough, because nothing should really be asleep because
  393. of the brief netif_stop_queue. If this isn't true or if it
  394. changes, use netif_wake_queue instead. */
  395. spin_unlock_irqrestore(&clip_priv->xoff_lock, flags);
  396. return NETDEV_TX_OK;
  397. }
  398. static int clip_mkip(struct atm_vcc *vcc, int timeout)
  399. {
  400. struct sk_buff_head *rq, queue;
  401. struct clip_vcc *clip_vcc;
  402. struct sk_buff *skb, *tmp;
  403. unsigned long flags;
  404. if (!vcc->push)
  405. return -EBADFD;
  406. clip_vcc = kmalloc(sizeof(struct clip_vcc), GFP_KERNEL);
  407. if (!clip_vcc)
  408. return -ENOMEM;
  409. pr_debug("%p vcc %p\n", clip_vcc, vcc);
  410. clip_vcc->vcc = vcc;
  411. vcc->user_back = clip_vcc;
  412. set_bit(ATM_VF_IS_CLIP, &vcc->flags);
  413. clip_vcc->entry = NULL;
  414. clip_vcc->xoff = 0;
  415. clip_vcc->encap = 1;
  416. clip_vcc->last_use = jiffies;
  417. clip_vcc->idle_timeout = timeout * HZ;
  418. clip_vcc->old_push = vcc->push;
  419. clip_vcc->old_pop = vcc->pop;
  420. vcc->push = clip_push;
  421. vcc->pop = clip_pop;
  422. __skb_queue_head_init(&queue);
  423. rq = &sk_atm(vcc)->sk_receive_queue;
  424. spin_lock_irqsave(&rq->lock, flags);
  425. skb_queue_splice_init(rq, &queue);
  426. spin_unlock_irqrestore(&rq->lock, flags);
  427. /* re-process everything received between connection setup and MKIP */
  428. skb_queue_walk_safe(&queue, skb, tmp) {
  429. if (!clip_devs) {
  430. atm_return(vcc, skb->truesize);
  431. kfree_skb(skb);
  432. } else {
  433. struct net_device *dev = skb->dev;
  434. unsigned int len = skb->len;
  435. skb_get(skb);
  436. clip_push(vcc, skb);
  437. dev->stats.rx_packets--;
  438. dev->stats.rx_bytes -= len;
  439. kfree_skb(skb);
  440. }
  441. }
  442. return 0;
  443. }
  444. static int clip_setentry(struct atm_vcc *vcc, __be32 ip)
  445. {
  446. struct neighbour *neigh;
  447. struct atmarp_entry *entry;
  448. int error;
  449. struct clip_vcc *clip_vcc;
  450. struct rtable *rt;
  451. if (vcc->push != clip_push) {
  452. pr_warning("non-CLIP VCC\n");
  453. return -EBADF;
  454. }
  455. clip_vcc = CLIP_VCC(vcc);
  456. if (!ip) {
  457. if (!clip_vcc->entry) {
  458. pr_err("hiding hidden ATMARP entry\n");
  459. return 0;
  460. }
  461. pr_debug("remove\n");
  462. unlink_clip_vcc(clip_vcc);
  463. return 0;
  464. }
  465. rt = ip_route_output(&init_net, ip, 0, 1, 0);
  466. if (IS_ERR(rt))
  467. return PTR_ERR(rt);
  468. neigh = __neigh_lookup(&clip_tbl, &ip, rt->dst.dev, 1);
  469. ip_rt_put(rt);
  470. if (!neigh)
  471. return -ENOMEM;
  472. entry = NEIGH2ENTRY(neigh);
  473. if (entry != clip_vcc->entry) {
  474. if (!clip_vcc->entry)
  475. pr_debug("add\n");
  476. else {
  477. pr_debug("update\n");
  478. unlink_clip_vcc(clip_vcc);
  479. }
  480. link_vcc(clip_vcc, entry);
  481. }
  482. error = neigh_update(neigh, llc_oui, NUD_PERMANENT,
  483. NEIGH_UPDATE_F_OVERRIDE | NEIGH_UPDATE_F_ADMIN);
  484. neigh_release(neigh);
  485. return error;
  486. }
  487. static const struct net_device_ops clip_netdev_ops = {
  488. .ndo_start_xmit = clip_start_xmit,
  489. };
  490. static void clip_setup(struct net_device *dev)
  491. {
  492. dev->netdev_ops = &clip_netdev_ops;
  493. dev->type = ARPHRD_ATM;
  494. dev->hard_header_len = RFC1483LLC_LEN;
  495. dev->mtu = RFC1626_MTU;
  496. dev->tx_queue_len = 100; /* "normal" queue (packets) */
  497. /* When using a "real" qdisc, the qdisc determines the queue */
  498. /* length. tx_queue_len is only used for the default case, */
  499. /* without any more elaborate queuing. 100 is a reasonable */
  500. /* compromise between decent burst-tolerance and protection */
  501. /* against memory hogs. */
  502. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  503. }
  504. static int clip_create(int number)
  505. {
  506. struct net_device *dev;
  507. struct clip_priv *clip_priv;
  508. int error;
  509. if (number != -1) {
  510. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  511. if (PRIV(dev)->number == number)
  512. return -EEXIST;
  513. } else {
  514. number = 0;
  515. for (dev = clip_devs; dev; dev = PRIV(dev)->next)
  516. if (PRIV(dev)->number >= number)
  517. number = PRIV(dev)->number + 1;
  518. }
  519. dev = alloc_netdev(sizeof(struct clip_priv), "", clip_setup);
  520. if (!dev)
  521. return -ENOMEM;
  522. clip_priv = PRIV(dev);
  523. sprintf(dev->name, "atm%d", number);
  524. spin_lock_init(&clip_priv->xoff_lock);
  525. clip_priv->number = number;
  526. error = register_netdev(dev);
  527. if (error) {
  528. free_netdev(dev);
  529. return error;
  530. }
  531. clip_priv->next = clip_devs;
  532. clip_devs = dev;
  533. pr_debug("registered (net:%s)\n", dev->name);
  534. return number;
  535. }
  536. static int clip_device_event(struct notifier_block *this, unsigned long event,
  537. void *arg)
  538. {
  539. struct net_device *dev = arg;
  540. if (!net_eq(dev_net(dev), &init_net))
  541. return NOTIFY_DONE;
  542. if (event == NETDEV_UNREGISTER) {
  543. neigh_ifdown(&clip_tbl, dev);
  544. return NOTIFY_DONE;
  545. }
  546. /* ignore non-CLIP devices */
  547. if (dev->type != ARPHRD_ATM || dev->netdev_ops != &clip_netdev_ops)
  548. return NOTIFY_DONE;
  549. switch (event) {
  550. case NETDEV_UP:
  551. pr_debug("NETDEV_UP\n");
  552. to_atmarpd(act_up, PRIV(dev)->number, 0);
  553. break;
  554. case NETDEV_GOING_DOWN:
  555. pr_debug("NETDEV_DOWN\n");
  556. to_atmarpd(act_down, PRIV(dev)->number, 0);
  557. break;
  558. case NETDEV_CHANGE:
  559. case NETDEV_CHANGEMTU:
  560. pr_debug("NETDEV_CHANGE*\n");
  561. to_atmarpd(act_change, PRIV(dev)->number, 0);
  562. break;
  563. }
  564. return NOTIFY_DONE;
  565. }
  566. static int clip_inet_event(struct notifier_block *this, unsigned long event,
  567. void *ifa)
  568. {
  569. struct in_device *in_dev;
  570. in_dev = ((struct in_ifaddr *)ifa)->ifa_dev;
  571. /*
  572. * Transitions are of the down-change-up type, so it's sufficient to
  573. * handle the change on up.
  574. */
  575. if (event != NETDEV_UP)
  576. return NOTIFY_DONE;
  577. return clip_device_event(this, NETDEV_CHANGE, in_dev->dev);
  578. }
  579. static struct notifier_block clip_dev_notifier = {
  580. .notifier_call = clip_device_event,
  581. };
  582. static struct notifier_block clip_inet_notifier = {
  583. .notifier_call = clip_inet_event,
  584. };
  585. static void atmarpd_close(struct atm_vcc *vcc)
  586. {
  587. pr_debug("\n");
  588. rtnl_lock();
  589. atmarpd = NULL;
  590. skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
  591. rtnl_unlock();
  592. pr_debug("(done)\n");
  593. module_put(THIS_MODULE);
  594. }
  595. static struct atmdev_ops atmarpd_dev_ops = {
  596. .close = atmarpd_close
  597. };
  598. static struct atm_dev atmarpd_dev = {
  599. .ops = &atmarpd_dev_ops,
  600. .type = "arpd",
  601. .number = 999,
  602. .lock = __SPIN_LOCK_UNLOCKED(atmarpd_dev.lock)
  603. };
  604. static int atm_init_atmarp(struct atm_vcc *vcc)
  605. {
  606. rtnl_lock();
  607. if (atmarpd) {
  608. rtnl_unlock();
  609. return -EADDRINUSE;
  610. }
  611. mod_timer(&idle_timer, jiffies + CLIP_CHECK_INTERVAL * HZ);
  612. atmarpd = vcc;
  613. set_bit(ATM_VF_META, &vcc->flags);
  614. set_bit(ATM_VF_READY, &vcc->flags);
  615. /* allow replies and avoid getting closed if signaling dies */
  616. vcc->dev = &atmarpd_dev;
  617. vcc_insert_socket(sk_atm(vcc));
  618. vcc->push = NULL;
  619. vcc->pop = NULL; /* crash */
  620. vcc->push_oam = NULL; /* crash */
  621. rtnl_unlock();
  622. return 0;
  623. }
  624. static int clip_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  625. {
  626. struct atm_vcc *vcc = ATM_SD(sock);
  627. int err = 0;
  628. switch (cmd) {
  629. case SIOCMKCLIP:
  630. case ATMARPD_CTRL:
  631. case ATMARP_MKIP:
  632. case ATMARP_SETENTRY:
  633. case ATMARP_ENCAP:
  634. if (!capable(CAP_NET_ADMIN))
  635. return -EPERM;
  636. break;
  637. default:
  638. return -ENOIOCTLCMD;
  639. }
  640. switch (cmd) {
  641. case SIOCMKCLIP:
  642. err = clip_create(arg);
  643. break;
  644. case ATMARPD_CTRL:
  645. err = atm_init_atmarp(vcc);
  646. if (!err) {
  647. sock->state = SS_CONNECTED;
  648. __module_get(THIS_MODULE);
  649. }
  650. break;
  651. case ATMARP_MKIP:
  652. err = clip_mkip(vcc, arg);
  653. break;
  654. case ATMARP_SETENTRY:
  655. err = clip_setentry(vcc, (__force __be32)arg);
  656. break;
  657. case ATMARP_ENCAP:
  658. err = clip_encap(vcc, arg);
  659. break;
  660. }
  661. return err;
  662. }
  663. static struct atm_ioctl clip_ioctl_ops = {
  664. .owner = THIS_MODULE,
  665. .ioctl = clip_ioctl,
  666. };
  667. #ifdef CONFIG_PROC_FS
  668. static void svc_addr(struct seq_file *seq, struct sockaddr_atmsvc *addr)
  669. {
  670. static int code[] = { 1, 2, 10, 6, 1, 0 };
  671. static int e164[] = { 1, 8, 4, 6, 1, 0 };
  672. if (*addr->sas_addr.pub) {
  673. seq_printf(seq, "%s", addr->sas_addr.pub);
  674. if (*addr->sas_addr.prv)
  675. seq_putc(seq, '+');
  676. } else if (!*addr->sas_addr.prv) {
  677. seq_printf(seq, "%s", "(none)");
  678. return;
  679. }
  680. if (*addr->sas_addr.prv) {
  681. unsigned char *prv = addr->sas_addr.prv;
  682. int *fields;
  683. int i, j;
  684. fields = *prv == ATM_AFI_E164 ? e164 : code;
  685. for (i = 0; fields[i]; i++) {
  686. for (j = fields[i]; j; j--)
  687. seq_printf(seq, "%02X", *prv++);
  688. if (fields[i + 1])
  689. seq_putc(seq, '.');
  690. }
  691. }
  692. }
  693. /* This means the neighbour entry has no attached VCC objects. */
  694. #define SEQ_NO_VCC_TOKEN ((void *) 2)
  695. static void atmarp_info(struct seq_file *seq, struct net_device *dev,
  696. struct atmarp_entry *entry, struct clip_vcc *clip_vcc)
  697. {
  698. unsigned long exp;
  699. char buf[17];
  700. int svc, llc, off;
  701. svc = ((clip_vcc == SEQ_NO_VCC_TOKEN) ||
  702. (sk_atm(clip_vcc->vcc)->sk_family == AF_ATMSVC));
  703. llc = ((clip_vcc == SEQ_NO_VCC_TOKEN) || clip_vcc->encap);
  704. if (clip_vcc == SEQ_NO_VCC_TOKEN)
  705. exp = entry->neigh->used;
  706. else
  707. exp = clip_vcc->last_use;
  708. exp = (jiffies - exp) / HZ;
  709. seq_printf(seq, "%-6s%-4s%-4s%5ld ",
  710. dev->name, svc ? "SVC" : "PVC", llc ? "LLC" : "NULL", exp);
  711. off = scnprintf(buf, sizeof(buf) - 1, "%pI4",
  712. &entry->ip);
  713. while (off < 16)
  714. buf[off++] = ' ';
  715. buf[off] = '\0';
  716. seq_printf(seq, "%s", buf);
  717. if (clip_vcc == SEQ_NO_VCC_TOKEN) {
  718. if (time_before(jiffies, entry->expires))
  719. seq_printf(seq, "(resolving)\n");
  720. else
  721. seq_printf(seq, "(expired, ref %d)\n",
  722. atomic_read(&entry->neigh->refcnt));
  723. } else if (!svc) {
  724. seq_printf(seq, "%d.%d.%d\n",
  725. clip_vcc->vcc->dev->number,
  726. clip_vcc->vcc->vpi, clip_vcc->vcc->vci);
  727. } else {
  728. svc_addr(seq, &clip_vcc->vcc->remote);
  729. seq_putc(seq, '\n');
  730. }
  731. }
  732. struct clip_seq_state {
  733. /* This member must be first. */
  734. struct neigh_seq_state ns;
  735. /* Local to clip specific iteration. */
  736. struct clip_vcc *vcc;
  737. };
  738. static struct clip_vcc *clip_seq_next_vcc(struct atmarp_entry *e,
  739. struct clip_vcc *curr)
  740. {
  741. if (!curr) {
  742. curr = e->vccs;
  743. if (!curr)
  744. return SEQ_NO_VCC_TOKEN;
  745. return curr;
  746. }
  747. if (curr == SEQ_NO_VCC_TOKEN)
  748. return NULL;
  749. curr = curr->next;
  750. return curr;
  751. }
  752. static void *clip_seq_vcc_walk(struct clip_seq_state *state,
  753. struct atmarp_entry *e, loff_t * pos)
  754. {
  755. struct clip_vcc *vcc = state->vcc;
  756. vcc = clip_seq_next_vcc(e, vcc);
  757. if (vcc && pos != NULL) {
  758. while (*pos) {
  759. vcc = clip_seq_next_vcc(e, vcc);
  760. if (!vcc)
  761. break;
  762. --(*pos);
  763. }
  764. }
  765. state->vcc = vcc;
  766. return vcc;
  767. }
  768. static void *clip_seq_sub_iter(struct neigh_seq_state *_state,
  769. struct neighbour *n, loff_t * pos)
  770. {
  771. struct clip_seq_state *state = (struct clip_seq_state *)_state;
  772. return clip_seq_vcc_walk(state, NEIGH2ENTRY(n), pos);
  773. }
  774. static void *clip_seq_start(struct seq_file *seq, loff_t * pos)
  775. {
  776. struct clip_seq_state *state = seq->private;
  777. state->ns.neigh_sub_iter = clip_seq_sub_iter;
  778. return neigh_seq_start(seq, pos, &clip_tbl, NEIGH_SEQ_NEIGH_ONLY);
  779. }
  780. static int clip_seq_show(struct seq_file *seq, void *v)
  781. {
  782. static char atm_arp_banner[] =
  783. "IPitf TypeEncp Idle IP address ATM address\n";
  784. if (v == SEQ_START_TOKEN) {
  785. seq_puts(seq, atm_arp_banner);
  786. } else {
  787. struct clip_seq_state *state = seq->private;
  788. struct neighbour *n = v;
  789. struct clip_vcc *vcc = state->vcc;
  790. atmarp_info(seq, n->dev, NEIGH2ENTRY(n), vcc);
  791. }
  792. return 0;
  793. }
  794. static const struct seq_operations arp_seq_ops = {
  795. .start = clip_seq_start,
  796. .next = neigh_seq_next,
  797. .stop = neigh_seq_stop,
  798. .show = clip_seq_show,
  799. };
  800. static int arp_seq_open(struct inode *inode, struct file *file)
  801. {
  802. return seq_open_net(inode, file, &arp_seq_ops,
  803. sizeof(struct clip_seq_state));
  804. }
  805. static const struct file_operations arp_seq_fops = {
  806. .open = arp_seq_open,
  807. .read = seq_read,
  808. .llseek = seq_lseek,
  809. .release = seq_release_net,
  810. .owner = THIS_MODULE
  811. };
  812. #endif
  813. static void atm_clip_exit_noproc(void);
  814. static int __init atm_clip_init(void)
  815. {
  816. neigh_table_init_no_netlink(&clip_tbl);
  817. clip_tbl_hook = &clip_tbl;
  818. register_atm_ioctl(&clip_ioctl_ops);
  819. register_netdevice_notifier(&clip_dev_notifier);
  820. register_inetaddr_notifier(&clip_inet_notifier);
  821. setup_timer(&idle_timer, idle_timer_check, 0);
  822. #ifdef CONFIG_PROC_FS
  823. {
  824. struct proc_dir_entry *p;
  825. p = proc_create("arp", S_IRUGO, atm_proc_root, &arp_seq_fops);
  826. if (!p) {
  827. pr_err("Unable to initialize /proc/net/atm/arp\n");
  828. atm_clip_exit_noproc();
  829. return -ENOMEM;
  830. }
  831. }
  832. #endif
  833. return 0;
  834. }
  835. static void atm_clip_exit_noproc(void)
  836. {
  837. struct net_device *dev, *next;
  838. unregister_inetaddr_notifier(&clip_inet_notifier);
  839. unregister_netdevice_notifier(&clip_dev_notifier);
  840. deregister_atm_ioctl(&clip_ioctl_ops);
  841. /* First, stop the idle timer, so it stops banging
  842. * on the table.
  843. */
  844. del_timer_sync(&idle_timer);
  845. /* Next, purge the table, so that the device
  846. * unregister loop below does not hang due to
  847. * device references remaining in the table.
  848. */
  849. neigh_ifdown(&clip_tbl, NULL);
  850. dev = clip_devs;
  851. while (dev) {
  852. next = PRIV(dev)->next;
  853. unregister_netdev(dev);
  854. free_netdev(dev);
  855. dev = next;
  856. }
  857. /* Now it is safe to fully shutdown whole table. */
  858. neigh_table_clear(&clip_tbl);
  859. clip_tbl_hook = NULL;
  860. }
  861. static void __exit atm_clip_exit(void)
  862. {
  863. remove_proc_entry("arp", atm_proc_root);
  864. atm_clip_exit_noproc();
  865. }
  866. module_init(atm_clip_init);
  867. module_exit(atm_clip_exit);
  868. MODULE_AUTHOR("Werner Almesberger");
  869. MODULE_DESCRIPTION("Classical/IP over ATM interface");
  870. MODULE_LICENSE("GPL");