Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
inet.c
Go to the documentation of this file.
1 /*
2  * ---------------------------------------------------------------------------
3  * FILE: inet.c
4  *
5  * PURPOSE:
6  * Routines related to IP address changes.
7  * Optional part of the porting exercise. It uses system network
8  * handlers to obtain the UniFi IP address and pass it to the SME
9  * using the unifi_sys_ip_configured_ind().
10  *
11  * Copyright (C) 2008-2009 Cambridge Silicon Radio Ltd.
12  *
13  * Refer to LICENSE.txt included with this source code for details on
14  * the license terms.
15  *
16  * ---------------------------------------------------------------------------
17  */
18 #include <linux/inetdevice.h>
19 #include <linux/notifier.h>
20 
21 #include "unifi_priv.h"
23 
24 /*
25  * The inet notifier is global and not per-netdev. To avoid having a
26  * notifier registered when there are no unifi devices present, it's
27  * registered after the first unifi network device is registered, and
28  * unregistered when the last unifi network device is unregistered.
29  */
30 
31 static atomic_t inet_notif_refs = ATOMIC_INIT(0);
32 
33 static int uf_inetaddr_event(struct notifier_block *notif, unsigned long event, void *ifa)
34 {
35  struct net_device *ndev;
37  struct in_ifaddr *if_addr;
38  netInterface_priv_t *InterfacePriv = (netInterface_priv_t *)NULL;
39 
40  if (!ifa || !((struct in_ifaddr *)ifa)->ifa_dev) {
41  unifi_trace(NULL, UDBG1, "uf_inetaddr_event (%lu) ifa=%p\n", event, ifa);
42  return NOTIFY_DONE;
43  }
44 
45  ndev = ((struct in_ifaddr *)ifa)->ifa_dev->dev;
46  InterfacePriv = (netInterface_priv_t*) netdev_priv(ndev);
47 
48  /* As the notifier is global, the call may be for a non-UniFi netdev.
49  * Therefore check the netdev_priv to make sure it's a known UniFi one.
50  */
51  if (uf_find_netdev_priv(InterfacePriv) == -1) {
52  unifi_trace(NULL, UDBG1, "uf_inetaddr_event (%lu) ndev=%p, other netdev_priv=%p\n",
53  event, ndev, InterfacePriv);
54  return NOTIFY_DONE;
55  }
56 
57  if (!InterfacePriv->privPtr) {
58  unifi_error(NULL, "uf_inetaddr_event null priv (%lu) ndev=%p, InterfacePriv=%p\n",
59  event, ndev, InterfacePriv);
60  return NOTIFY_DONE;
61  }
62 
63  priv = InterfacePriv->privPtr;
64  if_addr = (struct in_ifaddr *)ifa;
65 
66  /* If this event is for a UniFi device, notify the SME that an IP
67  * address has been added or removed. */
68  if (uf_find_priv(priv) != -1) {
69  switch (event) {
70  case NETDEV_UP:
71  unifi_info(priv, "IP address assigned for %s\n", priv->netdev[InterfacePriv->InterfaceTag]->name);
72  priv->sta_ip_address = if_addr->ifa_address;
73 #ifdef CSR_SUPPORT_WEXT
75 #endif
76  break;
77  case NETDEV_DOWN:
78  unifi_info(priv, "IP address removed for %s\n", priv->netdev[InterfacePriv->InterfaceTag]->name);
79  priv->sta_ip_address = 0xFFFFFFFF;
80 #ifdef CSR_SUPPORT_WEXT
82 #endif
83  break;
84  }
85  }
86 
87  return NOTIFY_DONE;
88 }
89 
90 static struct notifier_block uf_inetaddr_notifier = {
91  .notifier_call = uf_inetaddr_event,
92 };
93 
95 {
96  if (atomic_inc_return(&inet_notif_refs) == 1) {
97  register_inetaddr_notifier(&uf_inetaddr_notifier);
98  }
99 }
100 
102 {
103  if (atomic_dec_return(&inet_notif_refs) == 0) {
104  unregister_inetaddr_notifier(&uf_inetaddr_notifier);
105  }
106 }