~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/l3mdev/l3mdev.c

Version: ~ [ linux-5.13-rc7 ] ~ [ linux-5.12.12 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.45 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.127 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.195 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.237 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.273 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.273 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /net/l3mdev/l3mdev.c (Version linux-4.18.20) and /net/l3mdev/l3mdev.c (Version linux-5.9.16)


                                                   >>   1 // SPDX-License-Identifier: GPL-2.0-or-later
  1 /*                                                  2 /*
  2  * net/l3mdev/l3mdev.c - L3 master device impl      3  * net/l3mdev/l3mdev.c - L3 master device implementation
  3  * Copyright (c) 2015 Cumulus Networks              4  * Copyright (c) 2015 Cumulus Networks
  4  * Copyright (c) 2015 David Ahern <dsa@cumulus      5  * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  5  *                                             << 
  6  * This program is free software; you can redi << 
  7  * it under the terms of the GNU General Publi << 
  8  * the Free Software Foundation; either versio << 
  9  * (at your option) any later version.         << 
 10  */                                                 6  */
 11                                                     7 
 12 #include <linux/netdevice.h>                        8 #include <linux/netdevice.h>
 13 #include <net/fib_rules.h>                          9 #include <net/fib_rules.h>
 14 #include <net/l3mdev.h>                            10 #include <net/l3mdev.h>
 15                                                    11 
                                                   >>  12 static DEFINE_SPINLOCK(l3mdev_lock);
                                                   >>  13 
                                                   >>  14 struct l3mdev_handler {
                                                   >>  15         lookup_by_table_id_t dev_lookup;
                                                   >>  16 };
                                                   >>  17 
                                                   >>  18 static struct l3mdev_handler l3mdev_handlers[L3MDEV_TYPE_MAX + 1];
                                                   >>  19 
                                                   >>  20 static int l3mdev_check_type(enum l3mdev_type l3type)
                                                   >>  21 {
                                                   >>  22         if (l3type <= L3MDEV_TYPE_UNSPEC || l3type > L3MDEV_TYPE_MAX)
                                                   >>  23                 return -EINVAL;
                                                   >>  24 
                                                   >>  25         return 0;
                                                   >>  26 }
                                                   >>  27 
                                                   >>  28 int l3mdev_table_lookup_register(enum l3mdev_type l3type,
                                                   >>  29                                  lookup_by_table_id_t fn)
                                                   >>  30 {
                                                   >>  31         struct l3mdev_handler *hdlr;
                                                   >>  32         int res;
                                                   >>  33 
                                                   >>  34         res = l3mdev_check_type(l3type);
                                                   >>  35         if (res)
                                                   >>  36                 return res;
                                                   >>  37 
                                                   >>  38         hdlr = &l3mdev_handlers[l3type];
                                                   >>  39 
                                                   >>  40         spin_lock(&l3mdev_lock);
                                                   >>  41 
                                                   >>  42         if (hdlr->dev_lookup) {
                                                   >>  43                 res = -EBUSY;
                                                   >>  44                 goto unlock;
                                                   >>  45         }
                                                   >>  46 
                                                   >>  47         hdlr->dev_lookup = fn;
                                                   >>  48         res = 0;
                                                   >>  49 
                                                   >>  50 unlock:
                                                   >>  51         spin_unlock(&l3mdev_lock);
                                                   >>  52 
                                                   >>  53         return res;
                                                   >>  54 }
                                                   >>  55 EXPORT_SYMBOL_GPL(l3mdev_table_lookup_register);
                                                   >>  56 
                                                   >>  57 void l3mdev_table_lookup_unregister(enum l3mdev_type l3type,
                                                   >>  58                                     lookup_by_table_id_t fn)
                                                   >>  59 {
                                                   >>  60         struct l3mdev_handler *hdlr;
                                                   >>  61 
                                                   >>  62         if (l3mdev_check_type(l3type))
                                                   >>  63                 return;
                                                   >>  64 
                                                   >>  65         hdlr = &l3mdev_handlers[l3type];
                                                   >>  66 
                                                   >>  67         spin_lock(&l3mdev_lock);
                                                   >>  68 
                                                   >>  69         if (hdlr->dev_lookup == fn)
                                                   >>  70                 hdlr->dev_lookup = NULL;
                                                   >>  71 
                                                   >>  72         spin_unlock(&l3mdev_lock);
                                                   >>  73 }
                                                   >>  74 EXPORT_SYMBOL_GPL(l3mdev_table_lookup_unregister);
                                                   >>  75 
                                                   >>  76 int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type,
                                                   >>  77                                       struct net *net, u32 table_id)
                                                   >>  78 {
                                                   >>  79         lookup_by_table_id_t lookup;
                                                   >>  80         struct l3mdev_handler *hdlr;
                                                   >>  81         int ifindex = -EINVAL;
                                                   >>  82         int res;
                                                   >>  83 
                                                   >>  84         res = l3mdev_check_type(l3type);
                                                   >>  85         if (res)
                                                   >>  86                 return res;
                                                   >>  87 
                                                   >>  88         hdlr = &l3mdev_handlers[l3type];
                                                   >>  89 
                                                   >>  90         spin_lock(&l3mdev_lock);
                                                   >>  91 
                                                   >>  92         lookup = hdlr->dev_lookup;
                                                   >>  93         if (!lookup)
                                                   >>  94                 goto unlock;
                                                   >>  95 
                                                   >>  96         ifindex = lookup(net, table_id);
                                                   >>  97 
                                                   >>  98 unlock:
                                                   >>  99         spin_unlock(&l3mdev_lock);
                                                   >> 100 
                                                   >> 101         return ifindex;
                                                   >> 102 }
                                                   >> 103 EXPORT_SYMBOL_GPL(l3mdev_ifindex_lookup_by_table_id);
                                                   >> 104 
 16 /**                                               105 /**
 17  *      l3mdev_master_ifindex - get index of L    106  *      l3mdev_master_ifindex - get index of L3 master device
 18  *      @dev: targeted interface                  107  *      @dev: targeted interface
 19  */                                               108  */
 20                                                   109 
 21 int l3mdev_master_ifindex_rcu(const struct net    110 int l3mdev_master_ifindex_rcu(const struct net_device *dev)
 22 {                                                 111 {
 23         int ifindex = 0;                          112         int ifindex = 0;
 24                                                   113 
 25         if (!dev)                                 114         if (!dev)
 26                 return 0;                         115                 return 0;
 27                                                   116 
 28         if (netif_is_l3_master(dev)) {            117         if (netif_is_l3_master(dev)) {
 29                 ifindex = dev->ifindex;           118                 ifindex = dev->ifindex;
 30         } else if (netif_is_l3_slave(dev)) {      119         } else if (netif_is_l3_slave(dev)) {
 31                 struct net_device *master;        120                 struct net_device *master;
 32                 struct net_device *_dev = (str    121                 struct net_device *_dev = (struct net_device *)dev;
 33                                                   122 
 34                 /* netdev_master_upper_dev_get    123                 /* netdev_master_upper_dev_get_rcu calls
 35                  * list_first_or_null_rcu to w    124                  * list_first_or_null_rcu to walk the upper dev list.
 36                  * list_first_or_null_rcu does    125                  * list_first_or_null_rcu does not handle a const arg. We aren't
 37                  * making changes, just want t    126                  * making changes, just want the master device from that list so
 38                  * typecast to remove the cons    127                  * typecast to remove the const
 39                  */                               128                  */
 40                 master = netdev_master_upper_d    129                 master = netdev_master_upper_dev_get_rcu(_dev);
 41                 if (master)                       130                 if (master)
 42                         ifindex = master->ifin    131                         ifindex = master->ifindex;
 43         }                                         132         }
 44                                                   133 
 45         return ifindex;                           134         return ifindex;
 46 }                                                 135 }
 47 EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);     136 EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);
 48                                                   137 
 49 /**                                               138 /**
 50  *      l3mdev_fib_table - get FIB table id as !! 139  *      l3mdev_master_upper_ifindex_by_index - get index of upper l3 master
                                                   >> 140  *                                             device
                                                   >> 141  *      @net: network namespace for device index lookup
                                                   >> 142  *      @ifindex: targeted interface
                                                   >> 143  */
                                                   >> 144 int l3mdev_master_upper_ifindex_by_index_rcu(struct net *net, int ifindex)
                                                   >> 145 {
                                                   >> 146         struct net_device *dev;
                                                   >> 147 
                                                   >> 148         dev = dev_get_by_index_rcu(net, ifindex);
                                                   >> 149         while (dev && !netif_is_l3_master(dev))
                                                   >> 150                 dev = netdev_master_upper_dev_get(dev);
                                                   >> 151 
                                                   >> 152         return dev ? dev->ifindex : 0;
                                                   >> 153 }
                                                   >> 154 EXPORT_SYMBOL_GPL(l3mdev_master_upper_ifindex_by_index_rcu);
                                                   >> 155 
                                                   >> 156 /**
                                                   >> 157  *      l3mdev_fib_table_rcu - get FIB table id associated with an L3
 51  *                             master interfac    158  *                             master interface
 52  *      @dev: targeted interface                  159  *      @dev: targeted interface
 53  */                                               160  */
 54                                                   161 
 55 u32 l3mdev_fib_table_rcu(const struct net_devi    162 u32 l3mdev_fib_table_rcu(const struct net_device *dev)
 56 {                                                 163 {
 57         u32 tb_id = 0;                            164         u32 tb_id = 0;
 58                                                   165 
 59         if (!dev)                                 166         if (!dev)
 60                 return 0;                         167                 return 0;
 61                                                   168 
 62         if (netif_is_l3_master(dev)) {            169         if (netif_is_l3_master(dev)) {
 63                 if (dev->l3mdev_ops->l3mdev_fi    170                 if (dev->l3mdev_ops->l3mdev_fib_table)
 64                         tb_id = dev->l3mdev_op    171                         tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev);
 65         } else if (netif_is_l3_slave(dev)) {      172         } else if (netif_is_l3_slave(dev)) {
 66                 /* Users of netdev_master_uppe    173                 /* Users of netdev_master_upper_dev_get_rcu need non-const,
 67                  * but current inet_*type func    174                  * but current inet_*type functions take a const
 68                  */                               175                  */
 69                 struct net_device *_dev = (str    176                 struct net_device *_dev = (struct net_device *) dev;
 70                 const struct net_device *maste    177                 const struct net_device *master;
 71                                                   178 
 72                 master = netdev_master_upper_d    179                 master = netdev_master_upper_dev_get_rcu(_dev);
 73                 if (master &&                     180                 if (master &&
 74                     master->l3mdev_ops->l3mdev    181                     master->l3mdev_ops->l3mdev_fib_table)
 75                         tb_id = master->l3mdev    182                         tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
 76         }                                         183         }
 77                                                   184 
 78         return tb_id;                             185         return tb_id;
 79 }                                                 186 }
 80 EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);          187 EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
 81                                                   188 
 82 u32 l3mdev_fib_table_by_index(struct net *net,    189 u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
 83 {                                                 190 {
 84         struct net_device *dev;                   191         struct net_device *dev;
 85         u32 tb_id = 0;                            192         u32 tb_id = 0;
 86                                                   193 
 87         if (!ifindex)                             194         if (!ifindex)
 88                 return 0;                         195                 return 0;
 89                                                   196 
 90         rcu_read_lock();                          197         rcu_read_lock();
 91                                                   198 
 92         dev = dev_get_by_index_rcu(net, ifinde    199         dev = dev_get_by_index_rcu(net, ifindex);
 93         if (dev)                                  200         if (dev)
 94                 tb_id = l3mdev_fib_table_rcu(d    201                 tb_id = l3mdev_fib_table_rcu(dev);
 95                                                   202 
 96         rcu_read_unlock();                        203         rcu_read_unlock();
 97                                                   204 
 98         return tb_id;                             205         return tb_id;
 99 }                                                 206 }
100 EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);     207 EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);
101                                                   208 
102 /**                                               209 /**
103  *      l3mdev_link_scope_lookup - IPv6 route     210  *      l3mdev_link_scope_lookup - IPv6 route lookup based on flow for link
104  *                           local and multica    211  *                           local and multicast addresses
105  *      @net: network namespace for device ind    212  *      @net: network namespace for device index lookup
106  *      @fl6: IPv6 flow struct for lookup         213  *      @fl6: IPv6 flow struct for lookup
                                                   >> 214  *      This function does not hold refcnt on the returned dst.
                                                   >> 215  *      Caller must hold rcu_read_lock().
107  */                                               216  */
108                                                   217 
109 struct dst_entry *l3mdev_link_scope_lookup(str    218 struct dst_entry *l3mdev_link_scope_lookup(struct net *net,
110                                            str    219                                            struct flowi6 *fl6)
111 {                                                 220 {
112         struct dst_entry *dst = NULL;             221         struct dst_entry *dst = NULL;
113         struct net_device *dev;                   222         struct net_device *dev;
114                                                   223 
                                                   >> 224         WARN_ON_ONCE(!rcu_read_lock_held());
115         if (fl6->flowi6_oif) {                    225         if (fl6->flowi6_oif) {
116                 rcu_read_lock();               << 
117                                                << 
118                 dev = dev_get_by_index_rcu(net    226                 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
119                 if (dev && netif_is_l3_slave(d    227                 if (dev && netif_is_l3_slave(dev))
120                         dev = netdev_master_up    228                         dev = netdev_master_upper_dev_get_rcu(dev);
121                                                   229 
122                 if (dev && netif_is_l3_master(    230                 if (dev && netif_is_l3_master(dev) &&
123                     dev->l3mdev_ops->l3mdev_li    231                     dev->l3mdev_ops->l3mdev_link_scope_lookup)
124                         dst = dev->l3mdev_ops-    232                         dst = dev->l3mdev_ops->l3mdev_link_scope_lookup(dev, fl6);
125                                                << 
126                 rcu_read_unlock();             << 
127         }                                         233         }
128                                                   234 
129         return dst;                               235         return dst;
130 }                                                 236 }
131 EXPORT_SYMBOL_GPL(l3mdev_link_scope_lookup);      237 EXPORT_SYMBOL_GPL(l3mdev_link_scope_lookup);
132                                                   238 
133 /**                                               239 /**
134  *      l3mdev_fib_rule_match - Determine if f    240  *      l3mdev_fib_rule_match - Determine if flowi references an
135  *                              L3 master devi    241  *                              L3 master device
136  *      @net: network namespace for device ind    242  *      @net: network namespace for device index lookup
137  *      @fl:  flow struct                         243  *      @fl:  flow struct
138  */                                               244  */
139                                                   245 
140 int l3mdev_fib_rule_match(struct net *net, str    246 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
141                           struct fib_lookup_ar    247                           struct fib_lookup_arg *arg)
142 {                                                 248 {
143         struct net_device *dev;                   249         struct net_device *dev;
144         int rc = 0;                               250         int rc = 0;
145                                                   251 
146         rcu_read_lock();                          252         rcu_read_lock();
147                                                   253 
148         dev = dev_get_by_index_rcu(net, fl->fl    254         dev = dev_get_by_index_rcu(net, fl->flowi_oif);
149         if (dev && netif_is_l3_master(dev) &&     255         if (dev && netif_is_l3_master(dev) &&
150             dev->l3mdev_ops->l3mdev_fib_table)    256             dev->l3mdev_ops->l3mdev_fib_table) {
151                 arg->table = dev->l3mdev_ops->    257                 arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
152                 rc = 1;                           258                 rc = 1;
153                 goto out;                         259                 goto out;
154         }                                         260         }
155                                                   261 
156         dev = dev_get_by_index_rcu(net, fl->fl    262         dev = dev_get_by_index_rcu(net, fl->flowi_iif);
157         if (dev && netif_is_l3_master(dev) &&     263         if (dev && netif_is_l3_master(dev) &&
158             dev->l3mdev_ops->l3mdev_fib_table)    264             dev->l3mdev_ops->l3mdev_fib_table) {
159                 arg->table = dev->l3mdev_ops->    265                 arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
160                 rc = 1;                           266                 rc = 1;
161                 goto out;                         267                 goto out;
162         }                                         268         }
163                                                   269 
164 out:                                              270 out:
165         rcu_read_unlock();                        271         rcu_read_unlock();
166                                                   272 
167         return rc;                                273         return rc;
168 }                                                 274 }
169                                                   275 
170 void l3mdev_update_flow(struct net *net, struc    276 void l3mdev_update_flow(struct net *net, struct flowi *fl)
171 {                                                 277 {
172         struct net_device *dev;                   278         struct net_device *dev;
173         int ifindex;                              279         int ifindex;
174                                                   280 
175         rcu_read_lock();                          281         rcu_read_lock();
176                                                   282 
177         if (fl->flowi_oif) {                      283         if (fl->flowi_oif) {
178                 dev = dev_get_by_index_rcu(net    284                 dev = dev_get_by_index_rcu(net, fl->flowi_oif);
179                 if (dev) {                        285                 if (dev) {
180                         ifindex = l3mdev_maste    286                         ifindex = l3mdev_master_ifindex_rcu(dev);
181                         if (ifindex) {            287                         if (ifindex) {
182                                 fl->flowi_oif     288                                 fl->flowi_oif = ifindex;
183                                 fl->flowi_flag    289                                 fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
184                                 goto out;         290                                 goto out;
185                         }                         291                         }
186                 }                                 292                 }
187         }                                         293         }
188                                                   294 
189         if (fl->flowi_iif) {                      295         if (fl->flowi_iif) {
190                 dev = dev_get_by_index_rcu(net    296                 dev = dev_get_by_index_rcu(net, fl->flowi_iif);
191                 if (dev) {                        297                 if (dev) {
192                         ifindex = l3mdev_maste    298                         ifindex = l3mdev_master_ifindex_rcu(dev);
193                         if (ifindex) {            299                         if (ifindex) {
194                                 fl->flowi_iif     300                                 fl->flowi_iif = ifindex;
195                                 fl->flowi_flag    301                                 fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
196                         }                         302                         }
197                 }                                 303                 }
198         }                                         304         }
199                                                   305 
200 out:                                              306 out:
201         rcu_read_unlock();                        307         rcu_read_unlock();
202 }                                                 308 }
203 EXPORT_SYMBOL_GPL(l3mdev_update_flow);            309 EXPORT_SYMBOL_GPL(l3mdev_update_flow);
204                                                   310 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp