net_kern.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Copyright (C) 2001 Lennert Buytenhek (buytenh@gnu.org) and
  3. * James Leu (jleu@mindspring.net).
  4. * Copyright (C) 2001 by various other people who didn't put their name here.
  5. * Licensed under the GPL.
  6. */
  7. #include "linux/kernel.h"
  8. #include "linux/netdevice.h"
  9. #include "linux/rtnetlink.h"
  10. #include "linux/skbuff.h"
  11. #include "linux/socket.h"
  12. #include "linux/spinlock.h"
  13. #include "linux/module.h"
  14. #include "linux/init.h"
  15. #include "linux/etherdevice.h"
  16. #include "linux/list.h"
  17. #include "linux/inetdevice.h"
  18. #include "linux/ctype.h"
  19. #include "linux/bootmem.h"
  20. #include "linux/ethtool.h"
  21. #include "linux/platform_device.h"
  22. #include "asm/uaccess.h"
  23. #include "kern_util.h"
  24. #include "net_kern.h"
  25. #include "net_user.h"
  26. #include "mconsole_kern.h"
  27. #include "init.h"
  28. #include "irq_user.h"
  29. #include "irq_kern.h"
  30. static inline void set_ether_mac(struct net_device *dev, unsigned char *addr)
  31. {
  32. memcpy(dev->dev_addr, addr, ETH_ALEN);
  33. }
  34. #define DRIVER_NAME "uml-netdev"
  35. static DEFINE_SPINLOCK(opened_lock);
  36. static LIST_HEAD(opened);
  37. static int uml_net_rx(struct net_device *dev)
  38. {
  39. struct uml_net_private *lp = dev->priv;
  40. int pkt_len;
  41. struct sk_buff *skb;
  42. /* If we can't allocate memory, try again next round. */
  43. skb = dev_alloc_skb(dev->mtu);
  44. if (skb == NULL) {
  45. lp->stats.rx_dropped++;
  46. return 0;
  47. }
  48. skb->dev = dev;
  49. skb_put(skb, dev->mtu);
  50. skb_reset_mac_header(skb);
  51. pkt_len = (*lp->read)(lp->fd, &skb, lp);
  52. if (pkt_len > 0) {
  53. skb_trim(skb, pkt_len);
  54. skb->protocol = (*lp->protocol)(skb);
  55. netif_rx(skb);
  56. lp->stats.rx_bytes += skb->len;
  57. lp->stats.rx_packets++;
  58. return pkt_len;
  59. }
  60. kfree_skb(skb);
  61. return pkt_len;
  62. }
  63. static void uml_dev_close(struct work_struct *work)
  64. {
  65. struct uml_net_private *lp =
  66. container_of(work, struct uml_net_private, work);
  67. dev_close(lp->dev);
  68. }
  69. irqreturn_t uml_net_interrupt(int irq, void *dev_id)
  70. {
  71. struct net_device *dev = dev_id;
  72. struct uml_net_private *lp = dev->priv;
  73. int err;
  74. if(!netif_running(dev))
  75. return(IRQ_NONE);
  76. spin_lock(&lp->lock);
  77. while((err = uml_net_rx(dev)) > 0) ;
  78. if(err < 0) {
  79. printk(KERN_ERR
  80. "Device '%s' read returned %d, shutting it down\n",
  81. dev->name, err);
  82. /* dev_close can't be called in interrupt context, and takes
  83. * again lp->lock.
  84. * And dev_close() can be safely called multiple times on the
  85. * same device, since it tests for (dev->flags & IFF_UP). So
  86. * there's no harm in delaying the device shutdown.
  87. * Furthermore, the workqueue will not re-enqueue an already
  88. * enqueued work item. */
  89. schedule_work(&lp->work);
  90. goto out;
  91. }
  92. reactivate_fd(lp->fd, UM_ETH_IRQ);
  93. out:
  94. spin_unlock(&lp->lock);
  95. return IRQ_HANDLED;
  96. }
  97. static int uml_net_open(struct net_device *dev)
  98. {
  99. struct uml_net_private *lp = dev->priv;
  100. int err;
  101. if(lp->fd >= 0){
  102. err = -ENXIO;
  103. goto out;
  104. }
  105. lp->fd = (*lp->open)(&lp->user);
  106. if(lp->fd < 0){
  107. err = lp->fd;
  108. goto out;
  109. }
  110. err = um_request_irq(dev->irq, lp->fd, IRQ_READ, uml_net_interrupt,
  111. IRQF_DISABLED | IRQF_SHARED, dev->name, dev);
  112. if(err != 0){
  113. printk(KERN_ERR "uml_net_open: failed to get irq(%d)\n", err);
  114. err = -ENETUNREACH;
  115. goto out_close;
  116. }
  117. lp->tl.data = (unsigned long) &lp->user;
  118. netif_start_queue(dev);
  119. /* clear buffer - it can happen that the host side of the interface
  120. * is full when we get here. In this case, new data is never queued,
  121. * SIGIOs never arrive, and the net never works.
  122. */
  123. while((err = uml_net_rx(dev)) > 0) ;
  124. spin_lock(&opened_lock);
  125. list_add(&lp->list, &opened);
  126. spin_unlock(&opened_lock);
  127. return 0;
  128. out_close:
  129. if(lp->close != NULL) (*lp->close)(lp->fd, &lp->user);
  130. lp->fd = -1;
  131. out:
  132. return err;
  133. }
  134. static int uml_net_close(struct net_device *dev)
  135. {
  136. struct uml_net_private *lp = dev->priv;
  137. netif_stop_queue(dev);
  138. free_irq(dev->irq, dev);
  139. if(lp->close != NULL)
  140. (*lp->close)(lp->fd, &lp->user);
  141. lp->fd = -1;
  142. spin_lock(&opened_lock);
  143. list_del(&lp->list);
  144. spin_unlock(&opened_lock);
  145. return 0;
  146. }
  147. static int uml_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  148. {
  149. struct uml_net_private *lp = dev->priv;
  150. unsigned long flags;
  151. int len;
  152. netif_stop_queue(dev);
  153. spin_lock_irqsave(&lp->lock, flags);
  154. len = (*lp->write)(lp->fd, &skb, lp);
  155. if(len == skb->len) {
  156. lp->stats.tx_packets++;
  157. lp->stats.tx_bytes += skb->len;
  158. dev->trans_start = jiffies;
  159. netif_start_queue(dev);
  160. /* this is normally done in the interrupt when tx finishes */
  161. netif_wake_queue(dev);
  162. }
  163. else if(len == 0){
  164. netif_start_queue(dev);
  165. lp->stats.tx_dropped++;
  166. }
  167. else {
  168. netif_start_queue(dev);
  169. printk(KERN_ERR "uml_net_start_xmit: failed(%d)\n", len);
  170. }
  171. spin_unlock_irqrestore(&lp->lock, flags);
  172. dev_kfree_skb(skb);
  173. return 0;
  174. }
  175. static struct net_device_stats *uml_net_get_stats(struct net_device *dev)
  176. {
  177. struct uml_net_private *lp = dev->priv;
  178. return &lp->stats;
  179. }
  180. static void uml_net_set_multicast_list(struct net_device *dev)
  181. {
  182. if (dev->flags & IFF_PROMISC) return;
  183. else if (dev->mc_count) dev->flags |= IFF_ALLMULTI;
  184. else dev->flags &= ~IFF_ALLMULTI;
  185. }
  186. static void uml_net_tx_timeout(struct net_device *dev)
  187. {
  188. dev->trans_start = jiffies;
  189. netif_wake_queue(dev);
  190. }
  191. static int uml_net_set_mac(struct net_device *dev, void *addr)
  192. {
  193. struct uml_net_private *lp = dev->priv;
  194. struct sockaddr *hwaddr = addr;
  195. spin_lock_irq(&lp->lock);
  196. set_ether_mac(dev, hwaddr->sa_data);
  197. spin_unlock_irq(&lp->lock);
  198. return 0;
  199. }
  200. static int uml_net_change_mtu(struct net_device *dev, int new_mtu)
  201. {
  202. struct uml_net_private *lp = dev->priv;
  203. int err = 0;
  204. spin_lock_irq(&lp->lock);
  205. new_mtu = (*lp->set_mtu)(new_mtu, &lp->user);
  206. if(new_mtu < 0){
  207. err = new_mtu;
  208. goto out;
  209. }
  210. dev->mtu = new_mtu;
  211. out:
  212. spin_unlock_irq(&lp->lock);
  213. return err;
  214. }
  215. static void uml_net_get_drvinfo(struct net_device *dev,
  216. struct ethtool_drvinfo *info)
  217. {
  218. strcpy(info->driver, DRIVER_NAME);
  219. strcpy(info->version, "42");
  220. }
  221. static struct ethtool_ops uml_net_ethtool_ops = {
  222. .get_drvinfo = uml_net_get_drvinfo,
  223. .get_link = ethtool_op_get_link,
  224. };
  225. void uml_net_user_timer_expire(unsigned long _conn)
  226. {
  227. #ifdef undef
  228. struct connection *conn = (struct connection *)_conn;
  229. dprintk(KERN_INFO "uml_net_user_timer_expire [%p]\n", conn);
  230. do_connect(conn);
  231. #endif
  232. }
  233. static void setup_etheraddr(char *str, unsigned char *addr, char *name)
  234. {
  235. char *end;
  236. int i;
  237. if(str == NULL)
  238. goto random;
  239. for(i=0;i<6;i++){
  240. addr[i] = simple_strtoul(str, &end, 16);
  241. if((end == str) ||
  242. ((*end != ':') && (*end != ',') && (*end != '\0'))){
  243. printk(KERN_ERR
  244. "setup_etheraddr: failed to parse '%s' "
  245. "as an ethernet address\n", str);
  246. goto random;
  247. }
  248. str = end + 1;
  249. }
  250. if (is_multicast_ether_addr(addr)) {
  251. printk(KERN_ERR
  252. "Attempt to assign a multicast ethernet address to a "
  253. "device disallowed\n");
  254. goto random;
  255. }
  256. if (!is_valid_ether_addr(addr)) {
  257. printk(KERN_ERR
  258. "Attempt to assign an invalid ethernet address to a "
  259. "device disallowed\n");
  260. goto random;
  261. }
  262. if (!is_local_ether_addr(addr)) {
  263. printk(KERN_WARNING
  264. "Warning: attempt to assign a globally valid ethernet address to a "
  265. "device\n");
  266. printk(KERN_WARNING "You should better enable the 2nd rightmost bit "
  267. "in the first byte of the MAC, i.e. "
  268. "%02x:%02x:%02x:%02x:%02x:%02x\n",
  269. addr[0] | 0x02, addr[1], addr[2], addr[3], addr[4], addr[5]);
  270. }
  271. return;
  272. random:
  273. printk(KERN_INFO
  274. "Choosing a random ethernet address for device %s\n", name);
  275. random_ether_addr(addr);
  276. }
  277. static DEFINE_SPINLOCK(devices_lock);
  278. static LIST_HEAD(devices);
  279. static struct platform_driver uml_net_driver = {
  280. .driver = {
  281. .name = DRIVER_NAME,
  282. },
  283. };
  284. static int driver_registered;
  285. static void net_device_release(struct device *dev)
  286. {
  287. struct uml_net *device = dev->driver_data;
  288. struct net_device *netdev = device->dev;
  289. struct uml_net_private *lp = netdev->priv;
  290. if(lp->remove != NULL)
  291. (*lp->remove)(&lp->user);
  292. list_del(&device->list);
  293. kfree(device);
  294. free_netdev(netdev);
  295. }
  296. static void eth_configure(int n, void *init, char *mac,
  297. struct transport *transport)
  298. {
  299. struct uml_net *device;
  300. struct net_device *dev;
  301. struct uml_net_private *lp;
  302. int err, size;
  303. size = transport->private_size + sizeof(struct uml_net_private);
  304. device = kzalloc(sizeof(*device), GFP_KERNEL);
  305. if (device == NULL) {
  306. printk(KERN_ERR "eth_configure failed to allocate struct "
  307. "uml_net\n");
  308. return;
  309. }
  310. dev = alloc_etherdev(size);
  311. if (dev == NULL) {
  312. printk(KERN_ERR "eth_configure: failed to allocate struct "
  313. "net_device for eth%d\n", n);
  314. goto out_free_device;
  315. }
  316. INIT_LIST_HEAD(&device->list);
  317. device->index = n;
  318. /* If this name ends up conflicting with an existing registered
  319. * netdevice, that is OK, register_netdev{,ice}() will notice this
  320. * and fail.
  321. */
  322. snprintf(dev->name, sizeof(dev->name), "eth%d", n);
  323. setup_etheraddr(mac, device->mac, dev->name);
  324. printk(KERN_INFO "Netdevice %d ", n);
  325. printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
  326. device->mac[0], device->mac[1],
  327. device->mac[2], device->mac[3],
  328. device->mac[4], device->mac[5]);
  329. printk(": ");
  330. lp = dev->priv;
  331. /* This points to the transport private data. It's still clear, but we
  332. * must memset it to 0 *now*. Let's help the drivers. */
  333. memset(lp, 0, size);
  334. INIT_WORK(&lp->work, uml_dev_close);
  335. /* sysfs register */
  336. if (!driver_registered) {
  337. platform_driver_register(&uml_net_driver);
  338. driver_registered = 1;
  339. }
  340. device->pdev.id = n;
  341. device->pdev.name = DRIVER_NAME;
  342. device->pdev.dev.release = net_device_release;
  343. device->pdev.dev.driver_data = device;
  344. if(platform_device_register(&device->pdev))
  345. goto out_free_netdev;
  346. SET_NETDEV_DEV(dev,&device->pdev.dev);
  347. device->dev = dev;
  348. /*
  349. * These just fill in a data structure, so there's no failure
  350. * to be worried about.
  351. */
  352. (*transport->kern->init)(dev, init);
  353. *lp = ((struct uml_net_private)
  354. { .list = LIST_HEAD_INIT(lp->list),
  355. .dev = dev,
  356. .fd = -1,
  357. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0},
  358. .protocol = transport->kern->protocol,
  359. .open = transport->user->open,
  360. .close = transport->user->close,
  361. .remove = transport->user->remove,
  362. .read = transport->kern->read,
  363. .write = transport->kern->write,
  364. .add_address = transport->user->add_address,
  365. .delete_address = transport->user->delete_address,
  366. .set_mtu = transport->user->set_mtu });
  367. init_timer(&lp->tl);
  368. spin_lock_init(&lp->lock);
  369. lp->tl.function = uml_net_user_timer_expire;
  370. memcpy(lp->mac, device->mac, sizeof(lp->mac));
  371. if ((transport->user->init != NULL) &&
  372. ((*transport->user->init)(&lp->user, dev) != 0))
  373. goto out_unregister;
  374. set_ether_mac(dev, device->mac);
  375. dev->mtu = transport->user->max_packet;
  376. dev->open = uml_net_open;
  377. dev->hard_start_xmit = uml_net_start_xmit;
  378. dev->stop = uml_net_close;
  379. dev->get_stats = uml_net_get_stats;
  380. dev->set_multicast_list = uml_net_set_multicast_list;
  381. dev->tx_timeout = uml_net_tx_timeout;
  382. dev->set_mac_address = uml_net_set_mac;
  383. dev->change_mtu = uml_net_change_mtu;
  384. dev->ethtool_ops = &uml_net_ethtool_ops;
  385. dev->watchdog_timeo = (HZ >> 1);
  386. dev->irq = UM_ETH_IRQ;
  387. rtnl_lock();
  388. err = register_netdevice(dev);
  389. rtnl_unlock();
  390. if (err)
  391. goto out_undo_user_init;
  392. spin_lock(&devices_lock);
  393. list_add(&device->list, &devices);
  394. spin_unlock(&devices_lock);
  395. return;
  396. out_undo_user_init:
  397. if (transport->user->remove != NULL)
  398. (*transport->user->remove)(&lp->user);
  399. out_unregister:
  400. platform_device_unregister(&device->pdev);
  401. out_free_netdev:
  402. free_netdev(dev);
  403. out_free_device:
  404. kfree(device);
  405. }
  406. static struct uml_net *find_device(int n)
  407. {
  408. struct uml_net *device;
  409. struct list_head *ele;
  410. spin_lock(&devices_lock);
  411. list_for_each(ele, &devices){
  412. device = list_entry(ele, struct uml_net, list);
  413. if(device->index == n)
  414. goto out;
  415. }
  416. device = NULL;
  417. out:
  418. spin_unlock(&devices_lock);
  419. return device;
  420. }
  421. static int eth_parse(char *str, int *index_out, char **str_out,
  422. char **error_out)
  423. {
  424. char *end;
  425. int n, err = -EINVAL;;
  426. n = simple_strtoul(str, &end, 0);
  427. if(end == str){
  428. *error_out = "Bad device number";
  429. return err;
  430. }
  431. str = end;
  432. if(*str != '='){
  433. *error_out = "Expected '=' after device number";
  434. return err;
  435. }
  436. str++;
  437. if(find_device(n)){
  438. *error_out = "Device already configured";
  439. return err;
  440. }
  441. *index_out = n;
  442. *str_out = str;
  443. return 0;
  444. }
  445. struct eth_init {
  446. struct list_head list;
  447. char *init;
  448. int index;
  449. };
  450. static DEFINE_SPINLOCK(transports_lock);
  451. static LIST_HEAD(transports);
  452. /* Filled in during early boot */
  453. static LIST_HEAD(eth_cmd_line);
  454. static int check_transport(struct transport *transport, char *eth, int n,
  455. void **init_out, char **mac_out)
  456. {
  457. int len;
  458. len = strlen(transport->name);
  459. if(strncmp(eth, transport->name, len))
  460. return 0;
  461. eth += len;
  462. if(*eth == ',')
  463. eth++;
  464. else if(*eth != '\0')
  465. return 0;
  466. *init_out = kmalloc(transport->setup_size, GFP_KERNEL);
  467. if(*init_out == NULL)
  468. return 1;
  469. if(!transport->setup(eth, mac_out, *init_out)){
  470. kfree(*init_out);
  471. *init_out = NULL;
  472. }
  473. return 1;
  474. }
  475. void register_transport(struct transport *new)
  476. {
  477. struct list_head *ele, *next;
  478. struct eth_init *eth;
  479. void *init;
  480. char *mac = NULL;
  481. int match;
  482. spin_lock(&transports_lock);
  483. BUG_ON(!list_empty(&new->list));
  484. list_add(&new->list, &transports);
  485. spin_unlock(&transports_lock);
  486. list_for_each_safe(ele, next, &eth_cmd_line){
  487. eth = list_entry(ele, struct eth_init, list);
  488. match = check_transport(new, eth->init, eth->index, &init,
  489. &mac);
  490. if(!match)
  491. continue;
  492. else if(init != NULL){
  493. eth_configure(eth->index, init, mac, new);
  494. kfree(init);
  495. }
  496. list_del(&eth->list);
  497. }
  498. }
  499. static int eth_setup_common(char *str, int index)
  500. {
  501. struct list_head *ele;
  502. struct transport *transport;
  503. void *init;
  504. char *mac = NULL;
  505. int found = 0;
  506. spin_lock(&transports_lock);
  507. list_for_each(ele, &transports){
  508. transport = list_entry(ele, struct transport, list);
  509. if(!check_transport(transport, str, index, &init, &mac))
  510. continue;
  511. if(init != NULL){
  512. eth_configure(index, init, mac, transport);
  513. kfree(init);
  514. }
  515. found = 1;
  516. break;
  517. }
  518. spin_unlock(&transports_lock);
  519. return found;
  520. }
  521. static int eth_setup(char *str)
  522. {
  523. struct eth_init *new;
  524. char *error;
  525. int n, err;
  526. err = eth_parse(str, &n, &str, &error);
  527. if(err){
  528. printk(KERN_ERR "eth_setup - Couldn't parse '%s' : %s\n",
  529. str, error);
  530. return 1;
  531. }
  532. new = alloc_bootmem(sizeof(*new));
  533. if (new == NULL){
  534. printk("eth_init : alloc_bootmem failed\n");
  535. return 1;
  536. }
  537. INIT_LIST_HEAD(&new->list);
  538. new->index = n;
  539. new->init = str;
  540. list_add_tail(&new->list, &eth_cmd_line);
  541. return 1;
  542. }
  543. __setup("eth", eth_setup);
  544. __uml_help(eth_setup,
  545. "eth[0-9]+=<transport>,<options>\n"
  546. " Configure a network device.\n\n"
  547. );
  548. static int net_config(char *str, char **error_out)
  549. {
  550. int n, err;
  551. err = eth_parse(str, &n, &str, error_out);
  552. if(err)
  553. return err;
  554. /* This string is broken up and the pieces used by the underlying
  555. * driver. So, it is freed only if eth_setup_common fails.
  556. */
  557. str = kstrdup(str, GFP_KERNEL);
  558. if(str == NULL){
  559. *error_out = "net_config failed to strdup string";
  560. return -ENOMEM;
  561. }
  562. err = !eth_setup_common(str, n);
  563. if(err)
  564. kfree(str);
  565. return(err);
  566. }
  567. static int net_id(char **str, int *start_out, int *end_out)
  568. {
  569. char *end;
  570. int n;
  571. n = simple_strtoul(*str, &end, 0);
  572. if((*end != '\0') || (end == *str))
  573. return -1;
  574. *start_out = n;
  575. *end_out = n;
  576. *str = end;
  577. return n;
  578. }
  579. static int net_remove(int n, char **error_out)
  580. {
  581. struct uml_net *device;
  582. struct net_device *dev;
  583. struct uml_net_private *lp;
  584. device = find_device(n);
  585. if(device == NULL)
  586. return -ENODEV;
  587. dev = device->dev;
  588. lp = dev->priv;
  589. if(lp->fd > 0)
  590. return -EBUSY;
  591. unregister_netdev(dev);
  592. platform_device_unregister(&device->pdev);
  593. return 0;
  594. }
  595. static struct mc_device net_mc = {
  596. .list = LIST_HEAD_INIT(net_mc.list),
  597. .name = "eth",
  598. .config = net_config,
  599. .get_config = NULL,
  600. .id = net_id,
  601. .remove = net_remove,
  602. };
  603. static int uml_inetaddr_event(struct notifier_block *this, unsigned long event,
  604. void *ptr)
  605. {
  606. struct in_ifaddr *ifa = ptr;
  607. struct net_device *dev = ifa->ifa_dev->dev;
  608. struct uml_net_private *lp;
  609. void (*proc)(unsigned char *, unsigned char *, void *);
  610. unsigned char addr_buf[4], netmask_buf[4];
  611. if(dev->open != uml_net_open)
  612. return NOTIFY_DONE;
  613. lp = dev->priv;
  614. proc = NULL;
  615. switch (event){
  616. case NETDEV_UP:
  617. proc = lp->add_address;
  618. break;
  619. case NETDEV_DOWN:
  620. proc = lp->delete_address;
  621. break;
  622. }
  623. if(proc != NULL){
  624. memcpy(addr_buf, &ifa->ifa_address, sizeof(addr_buf));
  625. memcpy(netmask_buf, &ifa->ifa_mask, sizeof(netmask_buf));
  626. (*proc)(addr_buf, netmask_buf, &lp->user);
  627. }
  628. return NOTIFY_DONE;
  629. }
  630. /* uml_net_init shouldn't be called twice on two CPUs at the same time */
  631. struct notifier_block uml_inetaddr_notifier = {
  632. .notifier_call = uml_inetaddr_event,
  633. };
  634. static int uml_net_init(void)
  635. {
  636. struct list_head *ele;
  637. struct uml_net_private *lp;
  638. struct in_device *ip;
  639. struct in_ifaddr *in;
  640. mconsole_register_dev(&net_mc);
  641. register_inetaddr_notifier(&uml_inetaddr_notifier);
  642. /* Devices may have been opened already, so the uml_inetaddr_notifier
  643. * didn't get a chance to run for them. This fakes it so that
  644. * addresses which have already been set up get handled properly.
  645. */
  646. spin_lock(&opened_lock);
  647. list_for_each(ele, &opened){
  648. lp = list_entry(ele, struct uml_net_private, list);
  649. ip = lp->dev->ip_ptr;
  650. if(ip == NULL)
  651. continue;
  652. in = ip->ifa_list;
  653. while(in != NULL){
  654. uml_inetaddr_event(NULL, NETDEV_UP, in);
  655. in = in->ifa_next;
  656. }
  657. }
  658. spin_unlock(&opened_lock);
  659. return 0;
  660. }
  661. __initcall(uml_net_init);
  662. static void close_devices(void)
  663. {
  664. struct list_head *ele;
  665. struct uml_net_private *lp;
  666. spin_lock(&opened_lock);
  667. list_for_each(ele, &opened){
  668. lp = list_entry(ele, struct uml_net_private, list);
  669. free_irq(lp->dev->irq, lp->dev);
  670. if((lp->close != NULL) && (lp->fd >= 0))
  671. (*lp->close)(lp->fd, &lp->user);
  672. if(lp->remove != NULL)
  673. (*lp->remove)(&lp->user);
  674. }
  675. spin_unlock(&opened_lock);
  676. }
  677. __uml_exitcall(close_devices);
  678. struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
  679. {
  680. if((skb != NULL) && (skb_tailroom(skb) < extra)){
  681. struct sk_buff *skb2;
  682. skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
  683. dev_kfree_skb(skb);
  684. skb = skb2;
  685. }
  686. if(skb != NULL) skb_put(skb, extra);
  687. return(skb);
  688. }
  689. void iter_addresses(void *d, void (*cb)(unsigned char *, unsigned char *,
  690. void *),
  691. void *arg)
  692. {
  693. struct net_device *dev = d;
  694. struct in_device *ip = dev->ip_ptr;
  695. struct in_ifaddr *in;
  696. unsigned char address[4], netmask[4];
  697. if(ip == NULL) return;
  698. in = ip->ifa_list;
  699. while(in != NULL){
  700. memcpy(address, &in->ifa_address, sizeof(address));
  701. memcpy(netmask, &in->ifa_mask, sizeof(netmask));
  702. (*cb)(address, netmask, arg);
  703. in = in->ifa_next;
  704. }
  705. }
  706. int dev_netmask(void *d, void *m)
  707. {
  708. struct net_device *dev = d;
  709. struct in_device *ip = dev->ip_ptr;
  710. struct in_ifaddr *in;
  711. __be32 *mask_out = m;
  712. if(ip == NULL)
  713. return(1);
  714. in = ip->ifa_list;
  715. if(in == NULL)
  716. return(1);
  717. *mask_out = in->ifa_mask;
  718. return(0);
  719. }
  720. void *get_output_buffer(int *len_out)
  721. {
  722. void *ret;
  723. ret = (void *) __get_free_pages(GFP_KERNEL, 0);
  724. if(ret) *len_out = PAGE_SIZE;
  725. else *len_out = 0;
  726. return ret;
  727. }
  728. void free_output_buffer(void *buffer)
  729. {
  730. free_pages((unsigned long) buffer, 0);
  731. }
  732. int tap_setup_common(char *str, char *type, char **dev_name, char **mac_out,
  733. char **gate_addr)
  734. {
  735. char *remain;
  736. remain = split_if_spec(str, dev_name, mac_out, gate_addr, NULL);
  737. if(remain != NULL){
  738. printk("tap_setup_common - Extra garbage on specification : "
  739. "'%s'\n", remain);
  740. return(1);
  741. }
  742. return(0);
  743. }
  744. unsigned short eth_protocol(struct sk_buff *skb)
  745. {
  746. return(eth_type_trans(skb, skb->dev));
  747. }