net_kern.c 19 KB

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