cregit-Linux how code gets into the kernel

Release 4.8 net/ipv4/fib_frontend.c

Directory: net/ipv4
/*
 * INET         An implementation of the TCP/IP protocol suite for the LINUX
 *              operating system.  INET is implemented using the  BSD Socket
 *              interface as the means of communication with the user level.
 *
 *              IPv4 Forwarding Information Base: FIB frontend.
 *
 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 *              This program is free software; you can redistribute it and/or
 *              modify it under the terms of the GNU General Public License
 *              as published by the Free Software Foundation; either version
 *              2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/bitops.h>
#include <linux/capability.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/errno.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <linux/if_addr.h>
#include <linux/if_arp.h>
#include <linux/skbuff.h>
#include <linux/cache.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>

#include <net/ip.h>
#include <net/protocol.h>
#include <net/route.h>
#include <net/tcp.h>
#include <net/sock.h>
#include <net/arp.h>
#include <net/ip_fib.h>
#include <net/rtnetlink.h>
#include <net/xfrm.h>
#include <net/l3mdev.h>
#include <trace/events/fib.h>

#ifndef CONFIG_IP_MULTIPLE_TABLES


static int __net_init fib4_rules_init(struct net *net) { struct fib_table *local_table, *main_table; main_table = fib_trie_table(RT_TABLE_MAIN, NULL); if (!main_table) return -ENOMEM; local_table = fib_trie_table(RT_TABLE_LOCAL, main_table); if (!local_table) goto fail; hlist_add_head_rcu(&local_table->tb_hlist, &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]); hlist_add_head_rcu(&main_table->tb_hlist, &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]); return 0; fail: fib_free_table(main_table); return -ENOMEM; }

Contributors

PersonTokensPropCommitsCommitProp
denis v. lunevdenis v. lunev4845.71%440.00%
pavel emelianovpavel emelianov3432.38%110.00%
alexander duyckalexander duyck1918.10%330.00%
ian morrisian morris21.90%110.00%
david s. millerdavid s. miller21.90%110.00%
Total105100.00%10100.00%

#else
struct fib_table *fib_new_table(struct net *net, u32 id) { struct fib_table *tb, *alias = NULL; unsigned int h; if (id == 0) id = RT_TABLE_MAIN; tb = fib_get_table(net, id); if (tb) return tb; if (id == RT_TABLE_LOCAL) alias = fib_new_table(net, RT_TABLE_MAIN); tb = fib_trie_table(id, alias); if (!tb) return NULL; switch (id) { case RT_TABLE_LOCAL: rcu_assign_pointer(net->ipv4.fib_local, tb); break; case RT_TABLE_MAIN: rcu_assign_pointer(net->ipv4.fib_main, tb); break; case RT_TABLE_DEFAULT: rcu_assign_pointer(net->ipv4.fib_default, tb); break; default: break; } h = id & (FIB_TABLE_HASHSZ - 1); hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]); return tb; }

Contributors

PersonTokensPropCommitsCommitProp
patrick mchardypatrick mchardy5632.75%222.22%
david s. millerdavid s. miller4224.56%222.22%
alexander duyckalexander duyck3419.88%222.22%
pre-gitpre-git2816.37%111.11%
denis v. lunevdenis v. lunev116.43%222.22%
Total171100.00%9100.00%

EXPORT_SYMBOL_GPL(fib_new_table); /* caller must hold either rtnl or rcu read lock */
struct fib_table *fib_get_table(struct net *net, u32 id) { struct fib_table *tb; struct hlist_head *head; unsigned int h; if (id == 0) id = RT_TABLE_MAIN; h = id & (FIB_TABLE_HASHSZ - 1); head = &net->ipv4.fib_table_hash[h]; hlist_for_each_entry_rcu(tb, head, tb_hlist) { if (tb->tb_id == id) return tb; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
patrick mchardypatrick mchardy5767.06%125.00%
denis v. lunevdenis v. lunev2225.88%250.00%
pre-gitpre-git67.06%125.00%
Total85100.00%4100.00%

#endif /* CONFIG_IP_MULTIPLE_TABLES */
static void fib_replace_table(struct net *net, struct fib_table *old, struct fib_table *new) { #ifdef CONFIG_IP_MULTIPLE_TABLES switch (new->tb_id) { case RT_TABLE_LOCAL: rcu_assign_pointer(net->ipv4.fib_local, new); break; case RT_TABLE_MAIN: rcu_assign_pointer(net->ipv4.fib_main, new); break; case RT_TABLE_DEFAULT: rcu_assign_pointer(net->ipv4.fib_default, new); break; default: break; } #endif /* replace the old table in the hlist */ hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist); }

Contributors

PersonTokensPropCommitsCommitProp
alexander duyckalexander duyck95100.00%1100.00%
Total95100.00%1100.00%


int fib_unmerge(struct net *net) { struct fib_table *old, *new; /* attempt to fetch local table if it has been allocated */ old = fib_get_table(net, RT_TABLE_LOCAL); if (!old) return 0; new = fib_trie_unmerge(old); if (!new) return -ENOMEM; /* replace merged table with clean table */ if (new != old) { fib_replace_table(net, old, new); fib_free_table(old); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
alexander duyckalexander duyck78100.00%2100.00%
Total78100.00%2100.00%


static void fib_flush(struct net *net) { int flushed = 0; unsigned int h; for (h = 0; h < FIB_TABLE_HASHSZ; h++) { struct hlist_head *head = &net->ipv4.fib_table_hash[h]; struct hlist_node *tmp; struct fib_table *tb; hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) flushed += fib_table_flush(tb); } if (flushed) rt_cache_flush(net); }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git3641.86%114.29%
alexander duyckalexander duyck1820.93%114.29%
denis v. lunevdenis v. lunev1618.60%228.57%
patrick mchardypatrick mchardy1416.28%114.29%
adrian bunkadrian bunk11.16%114.29%
stephen hemmingerstephen hemminger11.16%114.29%
Total86100.00%7100.00%


void fib_flush_external(struct net *net) { struct fib_table *tb; struct hlist_head *head; unsigned int h; for (h = 0; h < FIB_TABLE_HASHSZ; h++) { head = &net->ipv4.fib_table_hash[h]; hlist_for_each_entry(tb, head, tb_hlist) fib_table_flush_external(tb); } }

Contributors

PersonTokensPropCommitsCommitProp
scott feldmanscott feldman64100.00%1100.00%
Total64100.00%1100.00%

/* * Find address type as if only "dev" was present in the system. If * on_dev is NULL then all interfaces are taken into consideration. */
static inline unsigned int __inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr, u32 tb_id) { struct flowi4 fl4 = { .daddr = addr }; struct fib_result res; unsigned int ret = RTN_BROADCAST; struct fib_table *table; if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) return RTN_BROADCAST; if (ipv4_is_multicast(addr)) return RTN_MULTICAST; rcu_read_lock(); table = fib_get_table(net, tb_id); if (table) { ret = RTN_UNICAST; if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) { if (!dev || dev == res.fi->fib_dev) ret = res.type; } } rcu_read_unlock(); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git6848.23%211.11%
laszlo attila tothlaszlo attila toth2215.60%15.56%
david s. millerdavid s. miller107.09%211.11%
pavel emelianovpavel emelianov96.38%15.56%
david aherndavid ahern85.67%211.11%
eric dumazeteric dumazet74.96%211.11%
eric w. biedermaneric w. biederman64.26%15.56%
alexander duyckalexander duyck42.84%15.56%
joe perchesjoe perches21.42%15.56%
jan engelhardtjan engelhardt10.71%15.56%
arnaldo carvalho de meloarnaldo carvalho de melo10.71%15.56%
denis v. lunevdenis v. lunev10.71%15.56%
al viroal viro10.71%15.56%
stephen hemmingerstephen hemminger10.71%15.56%
Total141100.00%18100.00%


unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id) { return __inet_dev_addr_type(net, NULL, addr, tb_id); }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern29100.00%2100.00%
Total29100.00%2100.00%

EXPORT_SYMBOL(inet_addr_type_table);
unsigned int inet_addr_type(struct net *net, __be32 addr) { return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL); }

Contributors

PersonTokensPropCommitsCommitProp
laszlo attila tothlaszlo attila toth1765.38%133.33%
eric w. biedermaneric w. biederman726.92%133.33%
david aherndavid ahern27.69%133.33%
Total26100.00%3100.00%

EXPORT_SYMBOL(inet_addr_type);
unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev, __be32 addr) { u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL; return __inet_dev_addr_type(net, dev, addr, rt_table); }

Contributors

PersonTokensPropCommitsCommitProp
laszlo attila tothlaszlo attila toth2353.49%120.00%
david aherndavid ahern1330.23%360.00%
eric w. biedermaneric w. biederman716.28%120.00%
Total43100.00%5100.00%

EXPORT_SYMBOL(inet_dev_addr_type); /* inet_addr_type with dev == NULL but using the table from a dev * if one is associated */
unsigned int inet_addr_type_dev_table(struct net *net, const struct net_device *dev, __be32 addr) { u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL; return __inet_dev_addr_type(net, NULL, addr, rt_table); }

Contributors

PersonTokensPropCommitsCommitProp
david aherndavid ahern43100.00%3100.00%
Total43100.00%3100.00%

EXPORT_SYMBOL(inet_addr_type_dev_table);
__be32 fib_compute_spec_dst(struct sk_buff *skb) { struct net_device *dev = skb->dev; struct in_device *in_dev; struct fib_result res; struct rtable *rt; struct net *net; int scope; rt = skb_rtable(skb); if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) == RTCF_LOCAL) return ip_hdr(skb)->daddr; in_dev = __in_dev_get_rcu(dev); BUG_ON(!in_dev); net = dev_net(dev); scope = RT_SCOPE_UNIVERSE; if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) { struct flowi4 fl4 = { .flowi4_iif = LOOPBACK_IFINDEX, .daddr = ip_hdr(skb)->saddr, .flowi4_tos = RT_TOS(ip_hdr(skb)->tos), .flowi4_scope = scope, .flowi4_mark = IN_DEV_SRC_VMARK(in_dev) ? skb->mark : 0, }; if (!fib_lookup(net, &fl4, &res, 0)) return FIB_RES_PREFSRC(net, res); } else { scope = RT_SCOPE_LINK; } return inet_select_addr(dev, ip_hdr(skb)->saddr, scope); }

Contributors

PersonTokensPropCommitsCommitProp
david s. millerdavid s. miller19391.04%233.33%
lance richardsonlance richardson125.66%116.67%
julian anastasovjulian anastasov41.89%116.67%
andy gospodarekandy gospodarek20.94%116.67%
pavel emelianovpavel emelianov10.47%116.67%
Total212100.00%6100.00%

/* Given (packet source, input interface) and optional (dst, oif, tos): * - (main) check, that source is valid i.e. not broadcast or our local * address. * - figure out what "logical" interface this packet arrived * and calculate "specific destination" address. * - check, that packet arrived from expected physical interface. * called with rcu_read_lock() */
static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, u8 tos, int oif, struct net_device *dev, int rpf, struct in_device *idev, u32 *itag) { int ret, no_addr; struct fib_result res; struct flowi4 fl4; struct net *net; bool dev_match; fl4.flowi4_oif = 0; fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev); if (!fl4.flowi4_iif) fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX; fl4.daddr = src; fl4.saddr = dst; fl4.flowi4_tos = tos; fl4.flowi4_scope = RT_SCOPE_UNIVERSE; fl4.flowi4_tun_key.tun_id = 0; fl4.flowi4_flags = 0; no_addr = idev->ifa_list == NULL; fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0; trace_fib_validate_source(dev, &fl4); net = dev_net(dev); if (fib_lookup(net, &fl4, &res, 0)) goto last_resort; if (res.type != RTN_UNICAST && (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev))) goto e_inval; if (!rpf && !fib_num_tclassid_users(dev_net(dev)) && (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) goto last_resort; fib_combine_itag(itag, &res); dev_match = false; #ifdef CONFIG_IP_ROUTE_MULTIPATH for (ret = 0; ret < res.fi->fib_nhs; ret++) { struct fib_nh *nh = &res.fi->fib_nh[ret]; if (nh->nh_dev == dev) { dev_match = true; break; } else if (l3mdev_master_ifindex_rcu(nh->nh_dev) == dev->ifindex) { dev_match = true; break; } } #else if (FIB_RES_DEV(res) == dev) dev_match = true; #endif if (dev_match) { ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST; return ret; } if (no_addr) goto last_resort; if (rpf == 1) goto e_rpf; fl4.flowi4_oif = dev->ifindex; ret = 0; if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) { if (res.type == RTN_UNICAST) ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST; } return ret; last_resort: if (rpf) goto e_rpf; *itag = 0; return 0; e_inval: return -EINVAL; e_rpf: return -EXDEV; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git17738.31%414.29%
david s. millerdavid s. miller12527.06%621.43%
david aherndavid ahern5111.04%414.29%
sebastien barresebastien barre388.23%13.57%
denis v. lunevdenis v. lunev153.25%27.14%
michael smithmichael smith132.81%13.57%
eric dumazeteric dumazet91.95%27.14%
thomas grafthomas graf81.73%13.57%
patrick mchardypatrick mchardy81.73%13.57%
jamal hadi salimjamal hadi salim40.87%13.57%
andy gospodarekandy gospodarek40.87%13.57%
cong wangcong wang30.65%13.57%
hideaki yoshifujihideaki yoshifuji30.65%13.57%
stephen hemmingerstephen hemminger20.43%13.57%
al viroal viro20.43%13.57%
Total462100.00%28100.00%

/* Ignore rp_filter for packets protected by IPsec. */
int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst, u8 tos, int oif, struct net_device *dev, struct in_device *idev, u32 *itag) { int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev); if (!r && !fib_num_tclassid_users(dev_net(dev)) && IN_DEV_ACCEPT_LOCAL(idev) && (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) { *itag = 0; return 0; } return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag); }

Contributors

PersonTokensPropCommitsCommitProp
david s. millerdavid s. miller9783.62%250.00%
julian anastasovjulian anastasov1412.07%125.00%
sebastien barresebastien barre54.31%125.00%
Total116100.00%4100.00%


static inline __be32 sk_extract_addr(struct sockaddr *addr) { return ((struct sockaddr_in *) addr)->sin_addr.s_addr; }

Contributors

PersonTokensPropCommitsCommitProp
thomas grafthomas graf2596.15%150.00%
al viroal viro13.85%150.00%
Total26100.00%2100.00%


static int put_rtax(struct nlattr *mx, int len, int type, u32 value) { struct nlattr *nla; nla = (struct nlattr *) ((char *) mx + len); nla->nla_type = type; nla->nla_len = nla_attr_size(4); *(u32 *) nla_data(nla) = value; return len + nla_total_size(4); }

Contributors

PersonTokensPropCommitsCommitProp
thomas grafthomas graf6989.61%150.00%
pre-gitpre-git810.39%150.00%
Total77100.00%2100.00%


static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt, struct fib_config *cfg) { __be32 addr; int plen; memset(cfg, 0, sizeof(*cfg)); cfg->fc_nlinfo.nl_net = net; if (rt->rt_dst.sa_family != AF_INET) return -EAFNOSUPPORT; /* * Check mask for validity: * a) it must be contiguous. * b) destination must have all host bits clear. * c) if application forgot to set correct family (AF_INET), * reject request unless it is absolutely clear i.e. * both family and mask are zero. */ plen = 32; addr = sk_extract_addr(&rt->rt_dst); if (!(rt->rt_flags & RTF_HOST)) { __be32 mask = sk_extract_addr(&rt->rt_genmask); if (rt->rt_genmask.sa_family != AF_INET) { if (mask || rt->rt_genmask.sa_family) return -EAFNOSUPPORT; } if (bad_mask(mask, addr)) return -EINVAL; plen = inet_mask_len(mask); } cfg->fc_dst_len = plen; cfg->fc_dst = addr; if (cmd != SIOCDELRT) { cfg->fc_nlflags = NLM_F_CREATE; cfg->fc_protocol = RTPROT_BOOT; } if (rt->rt_metric) cfg->fc_priority = rt->rt_metric - 1; if (rt->rt_flags & RTF_REJECT) { cfg->fc_scope = RT_SCOPE_HOST; cfg->fc_type = RTN_UNREACHABLE; return 0; } cfg->fc_scope = RT_SCOPE_NOWHERE; cfg->fc_type = RTN_UNICAST; if (rt->rt_dev) { char *colon; struct net_device *dev; char devname[IFNAMSIZ]; if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1)) return -EFAULT; devname[IFNAMSIZ-1] = 0; colon = strchr(devname, ':'); if (colon) *colon = 0; dev = __dev_get_by_name(net, devname); if (!dev) return -ENODEV; cfg->fc_oif = dev->ifindex; cfg->fc_table = l3mdev_fib_table(dev); if (colon) { struct in_ifaddr *ifa; struct in_device *in_dev = __in_dev_get_rtnl(dev); if (!in_dev) return -ENODEV; *colon = ':'; for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) if (strcmp(ifa->ifa_label, devname) == 0) break; if (!ifa) return -ENODEV; cfg->fc_prefsrc = ifa->ifa_local; } } addr = sk_extract_addr(&rt->rt_gateway); if (rt->rt_gateway.sa_family == AF_INET && addr) { unsigned int addr_type; cfg->fc_gw = addr; addr_type = inet_addr_type_table(net, addr, cfg->fc_table); if (rt->rt_flags & RTF_GATEWAY && addr_type == RTN_UNICAST) cfg->fc_scope = RT_SCOPE_UNIVERSE; } if (cmd == SIOCDELRT) return 0; if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw) return -EINVAL; if (cfg->fc_scope == RT_SCOPE_NOWHERE) cfg->fc_scope = RT_SCOPE_LINK; if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) { struct nlattr *mx; int len = 0; mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL); if (!mx) return -ENOMEM; if (rt->rt_flags & RTF_MTU) len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40); if (rt->rt_flags & RTF_WINDOW) len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window); if (rt->rt_flags & RTF_IRTT) len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3); cfg->fc_mx = mx; cfg->fc_mx_len = len; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
thomas grafthomas graf55884.16%19.09%
pre-gitpre-git598.90%218.18%
david aherndavid ahern182.71%19.09%
denis v. lunevdenis v. lunev142.11%218.18%
mark tomlinsonmark tomlinson91.36%19.09%
ian morrisian morris20.30%19.09%
al viroal viro20.30%218.18%
eric w. biedermaneric w. biederman10.15%19.09%
Total663100.00%11100.00%

/* * Handle IP routing ioctl calls. * These are used to manipulate the routing tables */
int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg) { struct fib_config cfg; struct rtentry rt; int err; switch (cmd) { case SIOCADDRT: /* Add a route */ case SIOCDELRT: /* Delete a route */ if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) return -EPERM; if (copy_from_user(&rt, arg, sizeof(rt))) return -EFAULT; rtnl_lock(); err = rtentry_to_fib_config(net, cmd, &rt, &cfg); if (err == 0) { struct fib_table *tb; if (cmd == SIOCDELRT) { tb = fib_get_table(net, cfg.fc_table); if (tb) err = fib_table_delete(tb, &cfg); else err = -ESRCH; } else { tb = fib_new_table(net, cfg.fc_table); if (tb) err = fib_table_insert(tb, &cfg); else err = -ENOBUFS; } /* allocated by rtentry_to_fib_config() */ kfree(cfg.fc_mx); } rtnl_unlock(); return err; } return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
thomas grafthomas graf18391.04%116.67%
denis v. lunevdenis v. lunev115.47%350.00%
eric w. biedermaneric w. biederman52.49%116.67%
stephen hemmingerstephen hemminger21.00%116.67%
Total201100.00%6100.00%

const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { [RTA_DST] = { .type = NLA_U32 }, [RTA_SRC] = { .type = NLA_U32 }, [RTA_IIF] = { .type = NLA_U32 }, [RTA_OIF] = { .type = NLA_U32 }, [RTA_GATEWAY] = { .type = NLA_U32 }, [RTA_PRIORITY] = { .type = NLA_U32 }, [RTA_PREFSRC]