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